chat.js 1022 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Make connection
  2. var socket = io.connect('http://localhost:4000');
  3. // Query DOM
  4. var message = document.getElementById('message'),
  5. nickName = document.getElementById('nick'),
  6. btn = document.getElementById('send'),
  7. output = document.getElementById('output'),
  8. feedback = document.getElementById('feedback');
  9. // Emit events
  10. btn.addEventListener('click', function () {
  11. socket.emit('chat', {
  12. message: message.value,
  13. nickName: nickName.value,
  14. });
  15. const youTubeReg =
  16. '(?:.+?)?(?:/v/|watch/|?v=|&v=|youtu.be/|/v=|^youtu.be/)([a-zA-Z0-9_-]{11})+';
  17. console.log(message.matches(youTubeReg));
  18. message.value = '';
  19. });
  20. message.addEventListener('keypress', function () {
  21. socket.emit('typing', nickName.value);
  22. });
  23. // Listen for events
  24. socket.on('chat', function (data) {
  25. feedback.innerHTML = '';
  26. output.innerHTML +=
  27. '<p><strong>' + data.nickName + ': </strong>' + data.message + '</p>';
  28. });
  29. socket.on('typing', function (data) {
  30. feedback.innerHTML = '<p><em>' + data + ' is typing a message...</em></p>';
  31. });