123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- angular.module('chatServices', [])
- .factory('Connection', function($rootScope, $timeout, $interval) {
- var WS = window['MozWebSocket'] ? MozWebSocket : WebSocket;
- var chatSocket = null;
- var ping = null;
- var service = {
- username: '',
- error: null,
- messages: [],
- isConnected: function() {
- return this.username != '';
- },
- addListener: function(f) {
- messageListeners.add(f);
- }
- };
- function wrap(func) {
- return function() {
- var args = arguments;
- $timeout(function() {
- func.apply(null, args);
- });
- }
- };
- service.connect = function(username) {
- chatSocket = new WS(jsRoutes.controllers.Application.chat(username).webSocketURL());
- chatSocket.onmessage = wrap(function(event) {
- var message = JSON.parse(event.data);
- console.log(message);
- if(message.kind != "pong") {
- $rootScope.$broadcast('ws:message', message);
- }
- });
- chatSocket.onopen = wrap(function() {
- $rootScope.$broadcast('ws:connected', username);
- service.username = username;
- ping = $interval(function() {
- service.send('/ping');
- }, 60000, 0, false);
- });
- chatSocket.onclose = wrap(function() {
- $rootScope.$broadcast('ws:disconnected');
- service.username = '';
- $interval.cancel(ping);
- });
- }
- service.disconnect = function() {
- chatSocket.close();
- chatSocket = null;
- }
- service.send = function(message) {
- chatSocket.send(JSON.stringify({text: message}));
- }
- return service;
- })
- .factory('Chat', function($rootScope, Connection) {
- var service = {
- username: '',
- messages: [],
- receive: function(message) {
- if(arguments.length == 3) {
- message = {
- kind: arguments[0],
- user: arguments[1],
- message: arguments[2]
- };
- }
- service.messages.push(message);
- },
- connect: Connection.connect,
- disconnect: Connection.disconnect,
- isConnected: Connection.isConnected,
- send: Connection.send
- };
- $rootScope.$on('ws:connected', function(event, username) {
- service.username = username;
- });
- $rootScope.$on('ws:message', function(event, message) {
- if(message.kind == "talk") {
- service.receive(message);
- }
- else if(message.kind == "join") {
- service.receive("join", message.user, " has joined.");
- }
- else if(message.kind == "quit") {
- service.receive("quit", message.user, " has left.");
- }
- });
- $rootScope.$on('ws:disconnected', function() {
- service.messages = [];
- service.username = '';
- });
- if(Connection.isConnected()) {
- service.username = Connection.username;
- }
- return service;
- })
|