Skip to content

Commit 2582f65

Browse files
author
Fabian Schmidt
committed
bug fix for 2 connections
1 parent df2ddff commit 2582f65

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

backend/messageHandler.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ export class MessageHandler{
88

99
constructor(socket : WSocketServer, messageFactory : MessageFactory){
1010
this.messageFactory = messageFactory;
11-
socket.addTextMessageHandler((message) => {
12-
this.handleMessages(message);
13-
});
11+
socket.addTextMessageHandler(this.handleMessages);
1412
}
1513

16-
handleMessages(message) {
14+
handleMessages = (message, connection) => {
1715
if (message.utf8Data) {
1816
Server.log("Client Got message " + message.utf8Data);
1917
try {
2018
var messageObj = this.messageFactory.getMessage(message.utf8Data);
2119
for(let handler of this.handler[messageObj.type]){
22-
handler(messageObj);
20+
handler(messageObj, connection);
2321
}
2422
} catch (err) {
2523
Server.log( "ERROR: " + err);

backend/server.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,28 @@ export class Server {
5555
return true;
5656
}
5757

58-
handleSongRequest = (messageObj) => {
58+
handleSongRequest = (messageObj, connection) => {
5959
var passed = (Date.now() - this.timeInMs) / 1000;
6060
var json = {} as any;
6161
console.log(SONG_REQUEST);
6262
json.source = this.currSong;
6363
json.time = passed;
6464
json.type = SONG_REQUEST;
65-
this.wSocket.send(json);
65+
connection.send(JSON.stringify(json));
6666
}
6767

68-
handleRTT = (messageObj) => {
68+
handleRTT = (messageObj, connection) => {
6969
console.log(RTT);
7070
var json = messageObj;
71-
this.wSocket.send(json);
71+
connection.send(JSON.stringify(json));
7272
}
7373

74-
handlePlayerDelay = (messageObj) => {
74+
handlePlayerDelay = (messageObj, connection) => {
7575
console.log(PLAYER_DELAY);
7676
var json = {} as any;
7777
json.source = this.currSong;
7878
json.type = PLAYER_DELAY;
79-
this.wSocket.send(json);
79+
connection.send(JSON.stringify(json));
8080
}
8181

8282
public static log(message : string) {

backend/wSocketServer.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
export class WSocketServer{
33
callbackFunction : Function;
4-
connection;
54
constructor(httpServer){
65
var WebSocketServer = require('websocket').server;
76
var server = new WebSocketServer({
@@ -13,16 +12,13 @@ export class WSocketServer{
1312

1413

1514
handleRequest = (request) => {
16-
this.connection = request.accept('echo-protocol', request.origin);
17-
this.connection.on('message',this.callbackFunction);
15+
var connection = request.accept('echo-protocol', request.origin);
16+
connection.on('message',(messageObj) => {
17+
this.callbackFunction(messageObj, connection);
18+
});
1819
}
1920

2021
addTextMessageHandler(handler : Function){
2122
this.callbackFunction = handler;
22-
}
23-
24-
send(obj)
25-
{
26-
this.connection.send(JSON.stringify(obj));
27-
}
23+
}
2824
}

0 commit comments

Comments
 (0)