-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Steve Gattuso
committed
Aug 18, 2013
0 parents
commit 2ee4d8d
Showing
5 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.swp | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var EventEmitter = require("events").EventEmitter; | ||
|
||
var ClientManager = function(pin_to_client) { | ||
var self = this; | ||
self.pin_to_client = pin_to_client; | ||
|
||
self.on("bindPin", function(connection, data) { | ||
console.log("pin bound"); | ||
self.pin_to_client[data.pin] = connection; | ||
}); | ||
} | ||
|
||
ClientManager.prototype.__proto__ = EventEmitter.prototype; | ||
|
||
exports.ClientManager = ClientManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "torval", | ||
"description": "Server for the Torval project", | ||
"author": "us", | ||
"version": "0.1.0", | ||
"dependencies": { | ||
"websocket": "1.0.x" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var EventEmitter = require("events").EventEmitter; | ||
|
||
var PhoneManager = function(pin_to_client) { | ||
var self = this; | ||
self.pin_to_client = pin_to_client; | ||
|
||
self.on("getPin", function(connection, data) { | ||
connection.pin = (Math.random(1) * 1000).toFixed(0); | ||
connection.sendEvent("setPin", { pin: connection.pin }); | ||
}); | ||
|
||
self.on("keyDown", function(connection, data){ | ||
console.log("[pin:" + connection.pin + "] Event keyDown on " + data.v); | ||
if(self.pin_to_client[connection.pin] == undefined) { | ||
return; | ||
} | ||
self.pin_to_client[connection.pin].sendEvent("keyDown", { v: data.v }); | ||
}); | ||
|
||
self.on("keyUp", function(connection, data){ | ||
console.log("[pin:" + connection.pin + "] Event keyUp on " + data.v); | ||
if(self.pin_to_client[connection.pin] == undefined) { | ||
return; | ||
} | ||
self.pin_to_client[connection.pin].sendEvent("keyUp", { v: data.v }); | ||
}); | ||
|
||
self.on("tilt", function(connection, data) { | ||
console.log("[pin:" + connection.pin + "] Event tilt on " + data.v); | ||
if(self.pin_to_client[connection.pin] == undefined) { | ||
return; | ||
} | ||
self.pin_to_client[connection.pin].sendEvent("tilt", { v: data.v }); | ||
}); | ||
} | ||
|
||
PhoneManager.prototype.__proto__ = EventEmitter.prototype; | ||
|
||
exports.PhoneManager = PhoneManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env node | ||
var WebSocketServer = require('websocket').server; | ||
var PhoneManager = require("./phonemanager").PhoneManager; | ||
var ClientManager = require("./clientmanager").ClientManager; | ||
var http = require('http'); | ||
|
||
var server = http.createServer(function(request, response) { | ||
console.log((new Date()) + ' received request for ' + request.url); | ||
response.writeHead(404); | ||
response.end(); | ||
}); | ||
|
||
var pin_to_client = {}; | ||
|
||
var phone_manager = new PhoneManager(pin_to_client); | ||
var client_manager = new ClientManager(pin_to_client); | ||
|
||
server.listen(8080, function() { | ||
console.log((new Date()) + " server is listening on port 8080"); | ||
}); | ||
|
||
wsServer = new WebSocketServer({ | ||
httpServer: server, | ||
autoAcceptConnections:false | ||
}); | ||
|
||
function handleRequest(protocol, manager) { | ||
return function(request) { | ||
try { | ||
if(request.requestedProtocols.indexOf(protocol) == -1) { | ||
return; | ||
} | ||
var conn = request.accept(protocol, request.origin); | ||
} catch(e) { | ||
return; | ||
} | ||
console.log((new Date()) + 'Connection accepted.'); | ||
|
||
conn.sendEvent = function(event, data) { | ||
data['e'] = event; | ||
conn.send(JSON.stringify(data)); | ||
} | ||
|
||
conn.on('message', function(data) { | ||
try { | ||
var msg = JSON.parse(data.utf8Data); | ||
} catch(e) { | ||
console.log("Invalid JSON from client"); | ||
return; | ||
} | ||
manager.emit(msg.e, conn, msg); | ||
}); | ||
|
||
conn.on('close', function(reasonCode, description) { | ||
console.log((new Date()) + ' Peer ' + conn.remoteAddress + ' disconnected'); | ||
}); | ||
}; | ||
} | ||
|
||
wsServer.on('request', handleRequest("client", client_manager)); | ||
wsServer.on('request', handleRequest("phone", phone_manager)); |