/** * 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;