Skip to content
This repository was archived by the owner on Apr 3, 2020. It is now read-only.

Commit ffcbb35

Browse files
author
KillaMaaki
committed
Added support for subscribing to client state changes. Adds new event type "stateChange" to client.addEventListener, which triggers state change callbacks whenever the client's state changes.
1 parent d38ebbf commit ffcbb35

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

browser/webext/netcode.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var _typeGetClientState = 106;
77
var _typeDestroyClient = 107;
88
var _typeClientDestroyed = 108;
99
var _typeCheckPresence = 109;
10+
var _typeClientStateChanged = 110;
1011
var _resultClientCreated = 201;
1112
var _resultSuccess = 202;
1213
var _resultError = 203;
@@ -146,6 +147,7 @@ window.netcode = {
146147
var clientId = args[0];
147148
_clients[clientId] = {
148149
_recvCallbacks: [],
150+
_stateChangeCallbacks: [],
149151
_isDestroyed: false,
150152
setTickRate: function(tickRate, callback) {
151153
if (this._isDestroyed) {
@@ -182,6 +184,9 @@ window.netcode = {
182184
if (type == "receive") {
183185
this._recvCallbacks.push(callback);
184186
}
187+
else if(type == "stateChange") {
188+
this._stateChangeCallbacks.push(callback);
189+
}
185190
},
186191
};
187192
callback(null, _clients[clientId]);
@@ -216,6 +221,15 @@ window.addEventListener("message", function(event) {
216221
_clients[clientId]._isDestroyed = true;
217222
delete _clients[clientId];
218223
// TODO: Fire event before deletion.
224+
} else if (resultType == _typeClientStateChanged) {
225+
// Client state changed
226+
var clientId = event.data.message[1];
227+
var stateStr = event.data.message[2];
228+
if(_clients[clientId] != undefined) {
229+
for (var i = 0; i < _clients[clientId]._stateChangeCallbacks.length; i++) {
230+
_clients[clientId]._stateChangeCallbacks[i](clientId, stateStr);
231+
}
232+
}
219233
} else {
220234
// Received a return value.
221235
var messageId = event.data.message[1];

netcode.io.host/Program.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ManagedClient
4242
const int TypeDestroyClient = 107;
4343
const int TypeClientDestroyed = 108;
4444
const int TypeCheckPresence = 109;
45+
const int TypeClientStateChanged = 110;
4546

4647
const int ResultClientCreated = 201;
4748
const int ResultSuccess = 202;
@@ -258,9 +259,18 @@ private static void ClientRun(object o)
258259
var id = (int)o;
259260
var managedClient = _clients[id];
260261

262+
// keep track of client state so we can notify application of state changes
263+
var prevState = managedClient.client.State;
264+
261265
while (managedClient.shouldRun)
262266
{
263267
managedClient.client.Update(managedClient.time);
268+
269+
if (managedClient.client.State != prevState)
270+
{
271+
prevState = managedClient.client.State;
272+
PostStateChange(id);
273+
}
264274

265275
if (managedClient.client.State == ClientState.Connected)
266276
{
@@ -302,5 +312,53 @@ private static void ClientRun(object o)
302312

303313
managedClient.client.Dispose();
304314
}
315+
316+
private static void PostStateChange( int id )
317+
{
318+
string state;
319+
switch (_clients[id].client.State)
320+
{
321+
case ClientState.Connected:
322+
state = "connected";
323+
break;
324+
case ClientState.ConnectionDenied:
325+
state = "connectionDenied";
326+
break;
327+
case ClientState.ConnectionRequestTimeout:
328+
state = "connectionRequestTimeout";
329+
break;
330+
case ClientState.ConnectionResponseTimeout:
331+
state = "connectionResponseTimeout";
332+
break;
333+
case ClientState.ConnectionTimedOut:
334+
state = "connectionTimedOut";
335+
break;
336+
case ClientState.ConnectTokenExpired:
337+
state = "connectTokenExpired";
338+
break;
339+
case ClientState.Disconnected:
340+
state = "disconnected";
341+
break;
342+
case ClientState.InvalidConnectToken:
343+
state = "invalidConnectToken";
344+
break;
345+
case ClientState.SendingConnectionRequest:
346+
state = "sendingConnectionRequest";
347+
break;
348+
case ClientState.SendingConnectionResponse:
349+
state = "sendingConnectionResponse";
350+
break;
351+
default:
352+
state = "unknown";
353+
break;
354+
}
355+
356+
WriteMessage(new JArray
357+
{
358+
JValue.FromObject(TypeClientStateChanged),
359+
JValue.FromObject(id),
360+
JValue.FromObject(state),
361+
});
362+
}
305363
}
306364
}

0 commit comments

Comments
 (0)