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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
var app = app || {};
class SocketService {
constructor() {
this.homeConnection = null;
this.gameConnection = null;
}
async initGameConnection() {
this.gameConnection = await new signalR.HubConnectionBuilder()
.withUrl(`${app.config.SERVICE_URL}/game`)
.build();
}
disconnectGame() {
this.gameConnection.stop();
this.gameConnection = null;
}
initGameReceivers() {
this.gameConnection.on('OnConnected', ([message]) => {
console.log(message);
});
this.gameConnection.on('StartGame', ([color]) => {
app.eventService.triggerEvent('GameStart', color)
});
this.gameConnection.on('OtherUp', () => {
app.eventService.triggerEvent('OtherUp');
});
this.gameConnection.on('OtherRight', () => {
app.eventService.triggerEvent('OtherRight');
});
this.gameConnection.on('OtherDown', () => {
app.eventService.triggerEvent('OtherDown');
});
this.gameConnection.on('OtherLeft', () => {
app.eventService.triggerEvent('OtherLeft');
});
this.gameConnection.on('OtherRotateLeft', () => {
app.eventService.triggerEvent('OtherRotateLeft');
});
this.gameConnection.on('OtherRotateRight', () => {
app.eventService.triggerEvent('OtherRotateRight');
});
this.gameConnection.on('GameState', (state) => {
app.eventService.triggerEvent('GameState', state);
});
this.gameConnection.on('GameEnd', (result) => {
app.eventService.triggerEvent('GameEnd', result);
});
}
initGame() {
return this.initGameConnection().then(() => {
this.initGameReceivers();
return this.start(this.gameConnection);
});
}
async initHomeConnection() {
this.homeConnection = await new signalR.HubConnectionBuilder()
.withUrl(`${app.config.SERVICE_URL}/home`)
.build();
}
disconnectHome() {
this.homeConnection.stop();
this.homeConnection = null;
}
initHomeReceivers() {
this.homeConnection.on('OnConnected', ([message]) => {
console.log(message);
});
this.homeConnection.on('OnLoggedIn', (users) => {
app.eventService.triggerEvent('OnLoggedIn', users);
});
this.homeConnection.on('OnLoggedOut', (users) => {
app.eventService.triggerEvent('OnLoggedOut', users);
});
this.homeConnection.on('ReceiveMessage', ([user, message]) => {
app.eventService.triggerEvent('OnReceiveMessage', [user, message]);
});
}
initHome() {
return this.initHomeConnection().then(() => {
this.initHomeReceivers();
return this.start(this.homeConnection);
});
}
start(connection) {
const starting = async () => {
try {
await connection.start();
} catch (e) {
console.log(e);
}
};
return starting().then(() => {});
}
send(location, method, ...args) {
if(location === 'home') {
console.log(this.homeConnection);
this.homeConnection?.invoke(method, ...args);
}
if(location === 'game') {
this.gameConnection?.invoke(method, ...args);
}
}
}
app.socketService = new SocketService();
|