-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathws_interface.js
55 lines (39 loc) · 1.38 KB
/
ws_interface.js
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
55
module.exports = function (dict_activeGames, most_recent_pin){
var events = require('events');
var eventEmitter = new events.EventEmitter();
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
// process HTTP request. Since we're writing just WebSockets server
// we don't have to implement anything.
});
server.listen(3014, function() { });
//.listen(3008,'127.0.0.1');
console.log("xx3 Listening port 3014")
// create the server
wsServer = new WebSocketServer({
httpServer: server
});
// WebSocket server
wsServer.on('request', function(request) {
var connection = request.accept(null, request.origin);
console.log("xx3 New connection")
// This is the most important callback for us, we'll handle
// all messages from users here.
connection.on('message', function(message) {
if (message.type === 'utf8') {
// process WebSocket message
console.log(message);
console.log(most_recent_pin.p);
var R = dict_activeGames[most_recent_pin.p];
var gameObj = R.GameInstance.getGameObject();
connection.send(JSON.stringify(gameObj));
eventEmitter.emit('message', message);
}
});
connection.on('close', function(connection) {
console.log("xx3 Connection Closed")
// close user connection
});
});
}