index_SOCKET.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>
  5. Static File Index.HTML
  6. </title>
  7. <script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js'></script>
  8. </head>
  9. <body>
  10. <div id='formContainer'>
  11. <input type="nick" name="nick" placeholder="nick" id="nick">
  12. <input type="message" name="message" placeholder="message" id="message">
  13. <button id="send" onclick="myFunction()"> SEND </button>
  14. </div>
  15. <script>
  16. const socket = io()
  17. socket.on('msg', msg => console.log(msg))
  18. let div = document.createElement("div")
  19. let img = document.createElement("img")
  20. const youtubeRegexp = /http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?/;
  21. const imageRegexp = /^ftp|http|https?:\/\/(?:[a-z\-]+\.)+[a-z]{2,6}(?:\/[^\/#?]+)+\.(?:jpe?g|gif|png)$/ig
  22. function myFunction() {
  23. var nick = document.getElementById('nick').value;
  24. var message = document.getElementById('message').value;
  25. if (message.match(youtubeRegexp)) {
  26. div.innerHTML = `<iframe width="560" height="315" src="https://www.youtube.com/embed/${message.match(youtubeRegexp)[1]}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`
  27. document.body.append(div)
  28. } else
  29. if (message.match(imageRegexp)) {
  30. //вышло с метчем сравнить ссылку на картинку
  31. //тут смешная картинка, которая отобразится на сервере :D
  32. //http://risovach.ru/upload/2016/04/mem/kogda-s-dedom_112267989_orig_.jpg
  33. console.log("match image!")
  34. img.src = `${message}`
  35. document.body.append(img)
  36. } else {
  37. socket.emit('msg', {
  38. nick: nick,
  39. message: message
  40. })
  41. }
  42. }
  43. socket.on('msg', msg => {
  44. var div_nick = document.createElement("div");
  45. div_nick.style.cssText = `color: black ;
  46. background-color: yellow;
  47. width: 100px;
  48. text-align: center;
  49. `;
  50. div_nick.innerHTML = ("<b>" + msg.nick + "</b>" + ": " + msg.message)
  51. document.body.append(div_nick)
  52. })
  53. </script>
  54. </body>
  55. </html>