Live (In a Volcano) community card game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

138 lines
3.9 KiB

// dtpweb-socket.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0
'use strict';
window.dtp = window.dtp || { };
import DtpWebLog from './dtp-log.js';
export default class DtpWebSocket {
constructor ( ) {
this.isConnected = false;
this.isAuthenticated = false;
this.joinedChannels = { };
this.log = new DtpWebLog('DtpWebSocket');
}
async connect (options) {
options = Object.assign({
withRetry: true,
withError: false,
}, options);
try {
const response = await fetch('/auth/socket-token');
if (!response.ok) {
this.retryConnect();
throw new Error('socket failed to connect');
}
const json = await response.json();
if (!json.success) {
this.retryConnect();
throw new Error(`failed to connect: ${json.message}`);
}
this.log.debug('connect', 'WebSocket connecting to Digital Telepresence Platform', json);
this.socket = io('/', {
transports: ['websocket'],
reconnection: false,
auth: {
token: json.token,
},
});
this.socket.on('connect', this.onSocketConnect.bind(this));
this.socket.on('authenticated', this.onSocketAuthenticated.bind(this));
this.socket.on('join-result', this.onJoinResult.bind(this));
this.socket.on('disconnect', this.onSocketDisconnect.bind(this));
} catch (error) {
this.log.error('connect', 'failed to connect', { error });
if (options.withRetry) {
this.retryConnect();
}
if (options.withError) {
throw error;
}
}
}
async onSocketConnect ( ) {
this.log.info('onSocketConnect', 'WebSocket connected');
this.isConnected = true;
}
async onSocketDisconnect (reason) {
this.isConnected = false;
this.socket.close();
delete this.socket;
if (!this.isAuthenticated) {
UIkit.modal.alert(`Failed to authenticate WebSocket session. Please log out, log back in, and try again.`);
return;
}
const REASONS = {
'io server disconnect': 'The server closed the connection',
'io client disconnect': 'The client closed the connection',
'ping timeout': 'The server is unreachable',
'transport close': 'Connection was lost or network changed',
'transport error': 'Server communication error, please try again.',
};
this.log.warn('onSocketDisconnect', 'WebSocket disconnected', { reason });
UIkit.modal.alert(`Disconnected: ${REASONS[reason]}`);
this.retryConnect();
}
retryConnect ( ) {
if (this.retryTimeout) {
return; // a retry is already in progress
}
this.retryTimeout = setTimeout(async ( ) => {
delete this.retryTimeout;
await this.connect();
}, 2000);
}
async onSocketAuthenticated (message) {
this.log.info('onSocketAuthenticated', message.message, { user: message.user });
this.isAuthenticated = true;
this.user = message.user;
this.joinChannel(message.user._id, 'user');
document.dispatchEvent(new Event('socketConnected'));
}
async joinChannel (channelId, channelType) {
this.log.info('joinChannel', 'joining channel', { channelId, channelType });
this.socket.emit('join', { channelId, channelType });
}
async isChannelJoined (channelId) {
return !!this.joinedChannels[channelId];
}
async onJoinResult (message) {
this.log.info('onJoinResult', 'channel joined', { message });
this.joinedChannels[message.channelId] = message;
}
async leaveChannel (channelId) {
this.log.info('leaveChannel', 'leaving channel', { channelId });
this.socket.emit('leave', { channelId });
if (this.joinedChannels[channelId]) {
delete this.joinedChannels[channelId];
}
}
async sendUserChat (channelId, content) {
this.log.info('sendUserChat', 'sending message to channel', { channelId, content });
this.socket.emit('user-chat', { channelId, content });
}
}