Changeset View
Changeset View
Standalone View
Standalone View
support/aphlict/server/lib/AphlictClientServer.js
| Show All 20 Lines | JX.install('AphlictClientServer', { | ||||
| properties: { | properties: { | ||||
| logger: null, | logger: null, | ||||
| }, | }, | ||||
| members: { | members: { | ||||
| _server: null, | _server: null, | ||||
| _lists: null, | _lists: null, | ||||
| getListenerList: function(path) { | getListenerList: function(instance) { | ||||
| if (!this._lists[path]) { | if (!this._lists[instance]) { | ||||
| this._lists[path] = new JX.AphlictListenerList(path); | this._lists[instance] = new JX.AphlictListenerList(instance); | ||||
| } | } | ||||
| return this._lists[path]; | return this._lists[instance]; | ||||
| }, | }, | ||||
| log: function() { | log: function() { | ||||
| var logger = this.getLogger(); | var logger = this.getLogger(); | ||||
| if (!logger) { | if (!logger) { | ||||
| return; | return; | ||||
| } | } | ||||
| Show All 11 Lines | members: { | ||||
| }, | }, | ||||
| listen: function() { | listen: function() { | ||||
| var self = this; | var self = this; | ||||
| var server = this._server.listen.apply(this._server, arguments); | var server = this._server.listen.apply(this._server, arguments); | ||||
| var wss = new WebSocket.Server({server: server}); | var wss = new WebSocket.Server({server: server}); | ||||
| wss.on('connection', function(ws) { | wss.on('connection', function(ws) { | ||||
| var path = url.parse(ws.upgradeReq.url).pathname; | var instance = url.parse(ws.upgradeReq.url).pathname; | ||||
| var listener = self.getListenerList(path).addListener(ws); | |||||
| instance = instance.replace(/\//g, ''); | |||||
| if (!instance.length) { | |||||
| instance = 'default'; | |||||
| } | |||||
| var listener = self.getListenerList(instance).addListener(ws); | |||||
| function log() { | function log() { | ||||
| self.log( | self.log( | ||||
| util.format('<%s>', listener.getDescription()) + | util.format('<%s>', listener.getDescription()) + | ||||
| ' ' + | ' ' + | ||||
| util.format.apply(null, arguments)); | util.format.apply(null, arguments)); | ||||
| } | } | ||||
| Show All 28 Lines | listen: function() { | ||||
| default: | default: | ||||
| log( | log( | ||||
| 'Unrecognized command "%s".', | 'Unrecognized command "%s".', | ||||
| message.command || '<undefined>'); | message.command || '<undefined>'); | ||||
| } | } | ||||
| }); | }); | ||||
| ws.on('close', function() { | ws.on('close', function() { | ||||
| self.getListenerList(path).removeListener(listener); | self.getListenerList(instance).removeListener(listener); | ||||
| log('Disconnected.'); | |||||
| }); | |||||
| wss.on('close', function() { | |||||
| self.getListenerList(path).removeListener(listener); | |||||
| log('Disconnected.'); | log('Disconnected.'); | ||||
| }); | }); | ||||
| wss.on('error', function(err) { | |||||
| log('Error: %s', err.message); | |||||
| }); | |||||
| }); | }); | ||||
| }, | } | ||||
| } | } | ||||
| }); | }); | ||||