Changeset View
Changeset View
Standalone View
Standalone View
support/aphlict/server/lib/AphlictClientServer.js
| 'use strict'; | 'use strict'; | ||||
| var JX = require('./javelin').JX; | var JX = require('./javelin').JX; | ||||
| require('./AphlictListenerList'); | require('./AphlictListenerList'); | ||||
| require('./AphlictLog'); | require('./AphlictLog'); | ||||
| var url = require('url'); | var url = require('url'); | ||||
| var util = require('util'); | var util = require('util'); | ||||
| var WebSocket = require('ws'); | var WebSocket = require('ws'); | ||||
| JX.install('AphlictClientServer', { | JX.install('AphlictClientServer', { | ||||
| construct: function(server) { | construct: function(server) { | ||||
| this.setLogger(new JX.AphlictLog()); | server.on('request', JX.bind(this, this._onrequest)); | ||||
| this._server = server; | this._server = server; | ||||
| this._lists = {}; | this._lists = {}; | ||||
| }, | }, | ||||
| properties: { | |||||
| logger: null, | |||||
| }, | |||||
| members: { | members: { | ||||
| _server: null, | _server: null, | ||||
| _lists: null, | _lists: null, | ||||
| getListenerList: function(path) { | getListenerList: function(path) { | ||||
| if (!this._lists[path]) { | if (!this._lists[path]) { | ||||
| this._lists[path] = new JX.AphlictListenerList(path); | this._lists[path] = new JX.AphlictListenerList(path); | ||||
| } | } | ||||
| return this._lists[path]; | return this._lists[path]; | ||||
| }, | }, | ||||
| log: function() { | |||||
| var logger = this.getLogger(); | |||||
| if (!logger) { | |||||
| return; | |||||
| } | |||||
| logger.log.apply(logger, arguments); | |||||
| return this; | |||||
| }, | |||||
| _onrequest: function(request, response) { | |||||
| // The websocket code upgrades connections before they get here, so | |||||
| // this only handles normal HTTP connections. We just fail them with | |||||
| // a 501 response. | |||||
| response.writeHead(501); | |||||
| response.end('HTTP/501 Use Websockets\n'); | |||||
| }, | |||||
| 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 path = url.parse(ws.upgradeReq.url).pathname; | ||||
| var listener = self.getListenerList(path).addListener(ws); | var listener = self.getListenerList(path).addListener(ws); | ||||
| function log() { | function log() { | ||||
| self.getLogger().log( | self.log( | ||||
| util.format('<%s>', listener.getDescription()) + | util.format('<%s>', listener.getDescription()) + | ||||
| ' ' + | ' ' + | ||||
| util.format.apply(null, arguments)); | util.format.apply(null, arguments)); | ||||
| } | } | ||||
| log('Connected from %s.', ws._socket.remoteAddress); | log('Connected from %s.', ws._socket.remoteAddress); | ||||
| ws.on('message', function(data) { | ws.on('message', function(data) { | ||||
| ▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | listen: function() { | ||||
| wss.on('error', function(err) { | wss.on('error', function(err) { | ||||
| log('Error: %s', err.message); | log('Error: %s', err.message); | ||||
| }); | }); | ||||
| }); | }); | ||||
| }, | }, | ||||
| }, | |||||
| properties: { | |||||
| logger: null, | |||||
| } | } | ||||
| }); | }); | ||||