forked from akaProxy/pong
-
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
Showing
8 changed files
with
270 additions
and
13 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
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
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,70 @@ | ||
function connectToUser(request){ | ||
var connection = request.accept(); | ||
var user = new ConnectedUser(); | ||
user.connection = connection; | ||
//user.id = generateUUID(); | ||
console.log("A user connected"); | ||
|
||
user.connection.on("message", function (message){ | ||
handleMessage(message); | ||
}); | ||
return user; | ||
} | ||
function ConnectedUser() { | ||
this.connection; | ||
this.id; | ||
|
||
this.send = function (message){ | ||
this.connection.sendUTF(message); | ||
} | ||
} | ||
|
||
function handleMessage(message){ | ||
if(message.type === "utf8"){ | ||
console.log("handling message: " + message.utf8Data); | ||
var string = message.utf8Data; | ||
var commands = string.split(";"); | ||
for (var a = 0; a < commands.length; a++) { | ||
var inputs = commands[a].split(":"); | ||
var method = inputs[0]; | ||
|
||
//This is the actual api | ||
|
||
if(method === "chat"){ | ||
|
||
console.log("chatting!"); | ||
var params = []; | ||
|
||
for (var i = 1; i < inputs.length; i++) { | ||
params.push(inputs[i]); | ||
}; | ||
|
||
chat(params); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
function chat(params){ | ||
var activeUsers = require("./backend").activeUsers; | ||
var recipiants = params[0].split(","); | ||
var message = params[1]; | ||
|
||
console.log(message); | ||
|
||
for (var a = 0; a < recipiants.length; a++) { | ||
console.log("in loop 1, recipiants: " + recipiants[a]); | ||
for (var i = 0; i < activeUsers.length; i++) { | ||
console.log("activeUser: " + activeUsers[i].id); | ||
if(recipiants[a] == activeUsers[i].id){ | ||
console.log("sending message: " + message + ", to: " + activeUsers[i].id); | ||
activeUsers[i].send(message); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
exports.handleMessage = handleMessage; | ||
exports.connectToUser = connectToUser; |
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,73 @@ | ||
var WebSocketServer = require('websocket').server; | ||
var APIInternal = require("./apiToInternal"); | ||
var httpServerFactory = require("./server"); | ||
var constants = require("./settings"); | ||
var httpServer1 = httpServerFactory.startAndGetServer(constants.PORT); | ||
var httpServerL = httpServerFactory.startAndGetServer(constants.PORT_L); | ||
var httpServerR = httpServerFactory.startAndGetServer(constants.PORT_R); | ||
|
||
var wsServer1 = new WebSocketServer({ | ||
httpServer: httpServer1, | ||
autoAcceptConnections: false | ||
}); | ||
var wsServerL = new WebSocketServer({ | ||
httpServer: httpServerL, | ||
autoAcceptConnections: false | ||
}); | ||
var wsServerR = new WebSocketServer({ | ||
httpServer: httpServerR, | ||
autoAcceptConnections: false | ||
}); | ||
|
||
var client = null; | ||
var left = null; | ||
var right = null; | ||
|
||
wsServer1.on("request", function(request){ | ||
if(!client) client = request.accept(); | ||
|
||
client.sendUTF("Hello! :) "); | ||
|
||
if(left) client.sendUTF(constants.PORT_L + " connected") | ||
if(right) client.sendUTF(constants.PORT_L + " connected") | ||
|
||
client.on("close", function(reasonData, description) { | ||
//Remove user from active users | ||
client = null; | ||
}); | ||
}); | ||
|
||
wsServerL.on("request", function(request){ | ||
if(!left) left = request.accept(); | ||
|
||
left.sendUTF("Hello! :) left"); | ||
|
||
left.on("message", function(message){ | ||
var newMsg = constants.PORT_L + " " + message.utf8Data; | ||
if(client) client.sendUTF(newMsg); | ||
console.log(newMsg); | ||
}); | ||
|
||
left.on("close", function(reasonData, description) { | ||
//Remove user from active users | ||
left = null; | ||
}); | ||
}); | ||
|
||
wsServerR.on("request", function(request){ | ||
if(!right) right = request.accept(); | ||
|
||
right.sendUTF("Hello! :) right"); | ||
|
||
right.on("message", function(message){ | ||
var newMsg = constants.PORT_R + " " + message.utf8Data; | ||
if(client) client.sendUTF(newMsg); | ||
console.log(newMsg); | ||
}); | ||
|
||
right.on("close", function(reasonData, description) { | ||
//Remove user from active users | ||
right = null; | ||
}); | ||
}); | ||
|
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,84 @@ | ||
var WebSocketServer = require('websocket').server; | ||
var APIInternal = require("./apiToInternal"); | ||
var httpServer = require("./server").startAndGetServer(); | ||
|
||
var wsServer = new WebSocketServer({ | ||
httpServer: httpServer, | ||
autoAcceptConnections: false | ||
}); | ||
|
||
var activeUsers = []; | ||
var ids = []; | ||
wsServer.on("request", function(request){ | ||
var user = APIInternal.connectToUser(request); | ||
user.id = generateUUID(); | ||
activeUsers.push(user); | ||
user.send("connection established. Currently " + activeUsers.length + " users connected"); | ||
user.send("Your id is: " + user.id); | ||
|
||
|
||
|
||
user.connection.on("close", function(reasonData, description) { | ||
//Remove user from active users | ||
for (var i = 0; i < activeUsers.length; i++) { | ||
var idOfActiveUser; | ||
if(activeUsers[i].id === user.id) { | ||
idOfActiveUser = activeUsers[i].id; | ||
activeUsers.splice(i, 1); | ||
} | ||
var index = ids.indexOf(idOfActiveUser); | ||
ids.splice(index, 1); | ||
}; | ||
}); | ||
}); | ||
|
||
|
||
|
||
function generateUUID(){ | ||
ids.sort(function(a, b){return a-b}); | ||
if(activeUsers.length > ids.length){ | ||
//Take empty slot | ||
ids = []; | ||
console.log("There are more active users than ids. Taking empty slot"); | ||
for (var i = 0; i < activeUsers.length; i++) { | ||
ids.push(activeUsers[i].id); | ||
}; | ||
ids.sort(function(a, b){return a-b}); | ||
|
||
if (ids[0] >= 2) { | ||
ids.push(1); | ||
ids.sort(function(a, b){return a-b}); | ||
return 1; | ||
} | ||
else { | ||
for (var i = 1; i < ids.length; i++) { | ||
if (ids[i] - ids[i-1] >= 2){ | ||
ids.push(ids[i-1] + 1); | ||
ids.sort(function(a, b){return a-b}); | ||
return ids[i-1] + 1; | ||
} | ||
}; | ||
console.log("Never more than one space between two ids. Giving top id"); | ||
ids.push(ids[ids.length - 1] + 1); | ||
console.log("ids length: " + ids.length); | ||
return ids[ids.length - 1]; | ||
} | ||
|
||
|
||
} | ||
else { | ||
|
||
if(ids.length === 0){ | ||
ids.push(1); | ||
return 1; | ||
} | ||
else { | ||
var heighestId = ids[ids.length - 1]; | ||
ids.push(heighestId + 1); | ||
return heighestId + 1; | ||
} | ||
} | ||
} | ||
|
||
exports.ids; | ||
exports.activeUsers = activeUsers; |
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,21 @@ | ||
var http = require("http"); | ||
var constants = require("./settings"); | ||
function startAndGetServer(port){ | ||
|
||
var httpServer = http.createServer(onRequest).listen(port, function(){ | ||
console.log((new Date()) + ' HTTP server is listening on port ' + port); | ||
}); | ||
|
||
function onRequest(request, response){ | ||
console.log((new Date()) + " Recieved request for " + request.url) | ||
response.writeHead(200, {"Content-Type": "text/plain"}); | ||
response.write("Hello World"); | ||
response.end(); | ||
} | ||
console.log("Server kör"); | ||
|
||
return httpServer; | ||
} | ||
exports.startAndGetServer = startAndGetServer; | ||
|
||
|
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,10 @@ | ||
function define(name, value) { | ||
Object.defineProperty(exports, name, { | ||
value: value, | ||
enumerable: true | ||
}); | ||
} | ||
|
||
define("PORT", 1337); | ||
define("PORT_L", 1338); | ||
define("PORT_R", 1339); |
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