Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. new Vue({
  2. el: '#app',
  3.  
  4. data: {
  5. ws: null, // Websocket
  6. newMsg: '',
  7. chatContent: '',
  8. email: null,
  9. username: null,
  10. password: null,
  11. joined: false
  12. },
  13.  
  14. created: function() {
  15. var self = this;
  16. this.ws = new WebSocket('ws://' + window.location.host + '/ws');
  17. this.ws.addEventListener('message', function(e) {
  18. var msg = JSON.parse(e.data);
  19. self.chatContent += '<div class="chip">'
  20. + '<img src="' + self.gravatarURL(msg.email) + '">'
  21. + msg.username
  22. + '</div>'
  23. + emojione.toImage(msg.message) + '<br/>';
  24. var element = document.getElementById('chat-messages');
  25. element.scrollTop = element.scrollHeight;
  26. });
  27. },
  28.  
  29. methods: {
  30. send: function () {
  31. if (this.newMsg != '') {
  32. this.ws.send(
  33. JSON.stringify({
  34. email: this.email,
  35. username: this.username,
  36. message: $('<p>').html(this.newMsg).text()
  37. }
  38. ));
  39. this.newMsg = '';
  40. }
  41. },
  42.  
  43. join: function () {
  44. if (!this.email) {
  45. Materialize.toast('You must enter an adress', 2000);
  46. return
  47. }
  48. if (!this.username) {
  49. Materialize.toast('You must enter a username', 2000);
  50. return
  51. }
  52. if (!this.password) {
  53. Materialize.toast('You must enter a password', 2000);
  54. return
  55. }
  56. this.email = $('<p>').html(this.email).text();
  57. this.username = $('<p>').html(this.username).text();
  58. this.password = $('<p>').html(this.password).text();
  59. this.ws.send(
  60. JSON.stringify({
  61. email: this.email,
  62. username: this.username,
  63. password: this.password,
  64. message: "login"
  65. }
  66. ));
  67. this.newMsg = '';
  68. this.joined = true;
  69. },
  70.  
  71. gravatarURL: function(email) {
  72. return 'http://www.gravatar.com/avatar/' + CryptoJS.MD5(email);
  73. }
  74. }
  75. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement