File tree 3 files changed +14
-20
lines changed
3 files changed +14
-20
lines changed Original file line number Diff line number Diff line change @@ -8,18 +8,16 @@ export class MessageHandler{
8
8
9
9
constructor ( socket : WSocketServer , messageFactory : MessageFactory ) {
10
10
this . messageFactory = messageFactory ;
11
- socket . addTextMessageHandler ( ( message ) => {
12
- this . handleMessages ( message ) ;
13
- } ) ;
11
+ socket . addTextMessageHandler ( this . handleMessages ) ;
14
12
}
15
13
16
- handleMessages ( message ) {
14
+ handleMessages = ( message , connection ) => {
17
15
if ( message . utf8Data ) {
18
16
Server . log ( "Client Got message " + message . utf8Data ) ;
19
17
try {
20
18
var messageObj = this . messageFactory . getMessage ( message . utf8Data ) ;
21
19
for ( let handler of this . handler [ messageObj . type ] ) {
22
- handler ( messageObj ) ;
20
+ handler ( messageObj , connection ) ;
23
21
}
24
22
} catch ( err ) {
25
23
Server . log ( "ERROR: " + err ) ;
Original file line number Diff line number Diff line change @@ -55,28 +55,28 @@ export class Server {
55
55
return true ;
56
56
}
57
57
58
- handleSongRequest = ( messageObj ) => {
58
+ handleSongRequest = ( messageObj , connection ) => {
59
59
var passed = ( Date . now ( ) - this . timeInMs ) / 1000 ;
60
60
var json = { } as any ;
61
61
console . log ( SONG_REQUEST ) ;
62
62
json . source = this . currSong ;
63
63
json . time = passed ;
64
64
json . type = SONG_REQUEST ;
65
- this . wSocket . send ( json ) ;
65
+ connection . send ( JSON . stringify ( json ) ) ;
66
66
}
67
67
68
- handleRTT = ( messageObj ) => {
68
+ handleRTT = ( messageObj , connection ) => {
69
69
console . log ( RTT ) ;
70
70
var json = messageObj ;
71
- this . wSocket . send ( json ) ;
71
+ connection . send ( JSON . stringify ( json ) ) ;
72
72
}
73
73
74
- handlePlayerDelay = ( messageObj ) => {
74
+ handlePlayerDelay = ( messageObj , connection ) => {
75
75
console . log ( PLAYER_DELAY ) ;
76
76
var json = { } as any ;
77
77
json . source = this . currSong ;
78
78
json . type = PLAYER_DELAY ;
79
- this . wSocket . send ( json ) ;
79
+ connection . send ( JSON . stringify ( json ) ) ;
80
80
}
81
81
82
82
public static log ( message : string ) {
Original file line number Diff line number Diff line change 1
1
2
2
export class WSocketServer {
3
3
callbackFunction : Function ;
4
- connection ;
5
4
constructor ( httpServer ) {
6
5
var WebSocketServer = require ( 'websocket' ) . server ;
7
6
var server = new WebSocketServer ( {
@@ -13,16 +12,13 @@ export class WSocketServer{
13
12
14
13
15
14
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
+ } ) ;
18
19
}
19
20
20
21
addTextMessageHandler ( handler : Function ) {
21
22
this . callbackFunction = handler ;
22
- }
23
-
24
- send ( obj )
25
- {
26
- this . connection . send ( JSON . stringify ( obj ) ) ;
27
- }
23
+ }
28
24
}
You can’t perform that action at this time.
0 commit comments