Skip to content

Commit 11c1724

Browse files
committed
Handle websocket authentication slightly differently to make errors easier to work with
1 parent 02c0d93 commit 11c1724

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

resources/scripts/components/server/WebsocketHandler.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default () => {
1111

1212
const updateToken = (uuid: string, socket: Websocket) => {
1313
getWebsocketToken(uuid)
14-
.then(data => socket.setToken(data.token))
14+
.then(data => socket.setToken(data.token, true))
1515
.catch(error => console.error(error));
1616
};
1717

@@ -24,7 +24,7 @@ export default () => {
2424

2525
const socket = new Websocket();
2626

27-
socket.on('SOCKET_OPEN', () => setConnectionState(true));
27+
socket.on('auth success', () => setConnectionState(true));
2828
socket.on('SOCKET_CLOSE', () => setConnectionState(false));
2929
socket.on('SOCKET_ERROR', () => setConnectionState(false));
3030
socket.on('status', (status) => setServerStatus(status));
@@ -38,7 +38,10 @@ export default () => {
3838

3939
getWebsocketToken(server.uuid)
4040
.then(data => {
41+
// Connect and then set the authentication token.
4142
socket.setToken(data.token).connect(data.socket);
43+
44+
// Once that is done, set the instance.
4245
setInstance(socket);
4346
})
4447
.catch(error => console.error(error));

resources/scripts/plugins/Websocket.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export class Websocket extends EventEmitter {
2323
private token: string = '';
2424

2525
// Connects to the websocket instance and sets the token for the initial request.
26-
connect (url: string) {
26+
connect (url: string): this {
2727
this.url = url;
28-
this.socket = new Sockette(`${this.url}?token=${this.token}`, {
28+
this.socket = new Sockette(`${this.url}`, {
2929
onmessage: e => {
3030
try {
3131
let { event, args } = JSON.parse(e.data);
@@ -34,11 +34,19 @@ export class Websocket extends EventEmitter {
3434
console.warn('Failed to parse incoming websocket message.', ex);
3535
}
3636
},
37-
onopen: () => this.emit('SOCKET_OPEN'),
38-
onreconnect: () => this.emit('SOCKET_RECONNECT'),
37+
onopen: () => {
38+
this.emit('SOCKET_OPEN');
39+
this.authenticate();
40+
},
41+
onreconnect: () => {
42+
this.emit('SOCKET_RECONNECT');
43+
this.authenticate();
44+
},
3945
onclose: () => this.emit('SOCKET_CLOSE'),
4046
onerror: () => this.emit('SOCKET_ERROR'),
4147
});
48+
49+
return this;
4250
}
4351

4452
// Returns the URL connected to for the socket.
@@ -48,11 +56,11 @@ export class Websocket extends EventEmitter {
4856

4957
// Sets the authentication token to use when sending commands back and forth
5058
// between the websocket instance.
51-
setToken (token: string): this {
59+
setToken (token: string, isUpdate = false): this {
5260
this.token = token;
5361

54-
if (this.url) {
55-
this.send('auth', token);
62+
if (isUpdate) {
63+
this.authenticate();
5664
}
5765

5866
return this;
@@ -63,6 +71,12 @@ export class Websocket extends EventEmitter {
6371
return this.token;
6472
}
6573

74+
authenticate () {
75+
if (this.url && this.token) {
76+
this.send('auth', this.token);
77+
}
78+
}
79+
6680
close (code?: number, reason?: string) {
6781
this.url = null;
6882
this.token = '';

0 commit comments

Comments
 (0)