Blame view

resources/js/livescores/ChatService.js 1.32 KB
e77200db5   nologostudio.ru   Initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  /**
   * Chat service.
   *
   * @return {Object}
   *         {Function} .on Binds to socket.io `on`
   *         {Function} .emit Binds to socket.io `emit`
   *         {Function} .say Sends a message to the room.
   *         {Function} .reconnect A socket utlility to reconnect when connection is lost.
   */
  const chat = function (cookie) {
      var socket;
  
      var chat = {
          connect: function () {
              connect();
  
              this.on = _.bind(socket.on, socket);
  
              this.emit = _.bind(socket.emit, socket);
  
              this.say = function (msg, room) {
                  console.log('Sending message');
                  chat.emit('chat', {room: room, msg: msg, timestamp: new Date()});
              };
          },
      };
  
  
      function connect() {
          socket = io(window.SITEURL, { path: '/chat/socket.io', forceNew: true });
  
          socket.on('connect', function () {
              socket.emit('set-user');
          });
  
          socket.on('set-user-ok', function () {
              socket.emit('join-room', 'global');
          });
  
          socket.on('set-guest-ok', function () {
              socket.emit('join-room-as-guest', 'global');
          });
  
          socket.on('join-room-ok', function () {
              socket.emit('recent-chats', 'global');
          });
      }
  
      return chat;
  };
  
  chat.$inject = ['cookie'];
  
  export default chat;