2020-09-25 18:29:22 +02:00
|
|
|
import {API_URL} from "../Enum/EnvironmentVariable";
|
2020-09-18 13:57:38 +02:00
|
|
|
import {
|
2020-09-24 17:24:37 +02:00
|
|
|
BatchMessage, GroupDeleteMessage, GroupUpdateMessage, ItemEventMessage,
|
2020-09-18 13:57:38 +02:00
|
|
|
PositionMessage,
|
2020-09-24 16:11:47 +02:00
|
|
|
SetPlayerDetailsMessage, UserJoinedMessage, UserLeftMessage, UserMovedMessage,
|
2020-09-18 13:57:38 +02:00
|
|
|
UserMovesMessage,
|
|
|
|
ViewportMessage
|
2020-09-25 18:29:22 +02:00
|
|
|
} from "../Messages/generated/messages_pb"
|
2020-05-15 22:40:06 +02:00
|
|
|
|
|
|
|
const SocketIo = require('socket.io-client');
|
|
|
|
import Socket = SocketIOClient.Socket;
|
2020-09-18 13:57:38 +02:00
|
|
|
import Direction = PositionMessage.Direction;
|
2020-09-25 18:29:22 +02:00
|
|
|
import {ProtobufClientUtils} from "../Network/ProtobufClientUtils";
|
|
|
|
import {
|
|
|
|
EventMessage,
|
|
|
|
GroupCreatedUpdatedMessageInterface, ItemEventMessageInterface,
|
|
|
|
MessageUserJoined,
|
|
|
|
RoomJoinedMessageInterface,
|
|
|
|
ViewportInterface, WebRtcDisconnectMessageInterface,
|
|
|
|
WebRtcSignalReceivedMessageInterface,
|
|
|
|
WebRtcSignalSentMessageInterface,
|
|
|
|
WebRtcStartMessageInterface
|
|
|
|
} from "./ConnexionModels";
|
|
|
|
|
|
|
|
|
|
|
|
export class RoomConnection implements RoomConnection {
|
2020-06-22 18:42:54 +02:00
|
|
|
private readonly socket: Socket;
|
2020-09-18 13:57:38 +02:00
|
|
|
private userId: number|null = null;
|
2020-09-18 15:51:15 +02:00
|
|
|
private batchCallbacks: Map<string, Function[]> = new Map<string, Function[]>();
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-09-25 18:29:22 +02:00
|
|
|
public constructor(token: string) {
|
2020-06-22 15:00:23 +02:00
|
|
|
|
|
|
|
this.socket = SocketIo(`${API_URL}`, {
|
|
|
|
query: {
|
2020-06-22 18:42:54 +02:00
|
|
|
token: token
|
2020-06-22 15:00:23 +02:00
|
|
|
},
|
|
|
|
reconnection: false // Reconnection is handled by the application itself
|
|
|
|
});
|
|
|
|
|
|
|
|
this.socket.on(EventMessage.MESSAGE_ERROR, (message: string) => {
|
|
|
|
console.error(EventMessage.MESSAGE_ERROR, message);
|
|
|
|
})
|
2020-09-15 10:06:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Messages inside batched messages are extracted and sent to listeners directly.
|
|
|
|
*/
|
2020-09-18 15:51:15 +02:00
|
|
|
this.socket.on(EventMessage.BATCH, (batchedMessagesBinary: ArrayBuffer) => {
|
2020-09-18 18:16:26 +02:00
|
|
|
const batchMessage = BatchMessage.deserializeBinary(new Uint8Array(batchedMessagesBinary));
|
2020-09-18 15:51:15 +02:00
|
|
|
|
|
|
|
for (const message of batchMessage.getPayloadList()) {
|
|
|
|
let event: string;
|
|
|
|
let payload;
|
|
|
|
if (message.hasUsermovedmessage()) {
|
|
|
|
event = EventMessage.USER_MOVED;
|
|
|
|
payload = message.getUsermovedmessage();
|
2020-09-24 10:05:16 +02:00
|
|
|
} else if (message.hasGroupupdatemessage()) {
|
|
|
|
event = EventMessage.GROUP_CREATE_UPDATE;
|
|
|
|
payload = message.getGroupupdatemessage();
|
|
|
|
} else if (message.hasGroupdeletemessage()) {
|
|
|
|
event = EventMessage.GROUP_DELETE;
|
|
|
|
payload = message.getGroupdeletemessage();
|
2020-09-24 14:50:28 +02:00
|
|
|
} else if (message.hasUserjoinedmessage()) {
|
|
|
|
event = EventMessage.JOIN_ROOM;
|
|
|
|
payload = message.getUserjoinedmessage();
|
2020-09-24 16:11:47 +02:00
|
|
|
} else if (message.hasUserleftmessage()) {
|
|
|
|
event = EventMessage.USER_LEFT;
|
|
|
|
payload = message.getUserleftmessage();
|
2020-09-24 17:24:37 +02:00
|
|
|
} else if (message.hasItemeventmessage()) {
|
|
|
|
event = EventMessage.ITEM_EVENT;
|
|
|
|
payload = message.getItemeventmessage();
|
2020-09-18 15:51:15 +02:00
|
|
|
} else {
|
|
|
|
throw new Error('Unexpected batch message type');
|
|
|
|
}
|
|
|
|
|
|
|
|
const listeners = this.batchCallbacks.get(event);
|
|
|
|
if (listeners === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-15 10:06:11 +02:00
|
|
|
for (const listener of listeners) {
|
2020-09-18 15:51:15 +02:00
|
|
|
listener(payload);
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
2020-09-25 18:29:22 +02:00
|
|
|
|
2020-09-28 15:02:37 +02:00
|
|
|
public emitPlayerDetailsMessage(userName: string, characterLayersSelected: string[]) {
|
2020-09-25 18:29:22 +02:00
|
|
|
const message = new SetPlayerDetailsMessage();
|
2020-09-28 15:02:37 +02:00
|
|
|
message.setName(userName);
|
2020-09-25 18:29:22 +02:00
|
|
|
message.setCharacterlayersList(characterLayersSelected);
|
|
|
|
this.socket.emit(EventMessage.SET_PLAYER_DETAILS, message.serializeBinary().buffer, (id: number) => {
|
|
|
|
this.userId = id;
|
|
|
|
});
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
2020-05-10 17:31:27 +02:00
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
public closeConnection(): void {
|
|
|
|
this.socket?.close();
|
2020-06-03 11:55:31 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-09-16 11:41:03 +02:00
|
|
|
public joinARoom(roomId: string, startX: number, startY: number, direction: string, moving: boolean, viewport: ViewportInterface): Promise<RoomJoinedMessageInterface> {
|
2020-07-27 22:36:07 +02:00
|
|
|
const promise = new Promise<RoomJoinedMessageInterface>((resolve, reject) => {
|
2020-09-15 16:21:41 +02:00
|
|
|
this.socket.emit(EventMessage.JOIN_ROOM, {
|
|
|
|
roomId,
|
|
|
|
position: {x: startX, y: startY, direction, moving },
|
|
|
|
viewport,
|
2020-09-16 11:41:03 +02:00
|
|
|
}, (roomJoinedMessage: RoomJoinedMessageInterface) => {
|
|
|
|
resolve(roomJoinedMessage);
|
2020-09-15 16:21:41 +02:00
|
|
|
});
|
2020-06-22 11:58:07 +02:00
|
|
|
})
|
|
|
|
return promise;
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-09-15 10:06:11 +02:00
|
|
|
public sharePosition(x : number, y : number, direction : string, moving: boolean, viewport: ViewportInterface) : void{
|
2020-04-07 20:46:30 +02:00
|
|
|
if(!this.socket){
|
|
|
|
return;
|
|
|
|
}
|
2020-09-18 13:57:38 +02:00
|
|
|
const positionMessage = new PositionMessage();
|
|
|
|
positionMessage.setX(Math.floor(x));
|
|
|
|
positionMessage.setY(Math.floor(y));
|
|
|
|
let directionEnum: PositionMessage.DirectionMap[keyof PositionMessage.DirectionMap];
|
|
|
|
switch (direction) {
|
|
|
|
case 'up':
|
|
|
|
directionEnum = Direction.UP;
|
|
|
|
break;
|
|
|
|
case 'down':
|
|
|
|
directionEnum = Direction.DOWN;
|
|
|
|
break;
|
|
|
|
case 'left':
|
|
|
|
directionEnum = Direction.LEFT;
|
|
|
|
break;
|
|
|
|
case 'right':
|
|
|
|
directionEnum = Direction.RIGHT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Unexpected direction");
|
|
|
|
}
|
|
|
|
positionMessage.setDirection(directionEnum);
|
|
|
|
positionMessage.setMoving(moving);
|
|
|
|
|
|
|
|
const viewportMessage = new ViewportMessage();
|
|
|
|
viewportMessage.setLeft(Math.floor(viewport.left));
|
|
|
|
viewportMessage.setRight(Math.floor(viewport.right));
|
|
|
|
viewportMessage.setTop(Math.floor(viewport.top));
|
|
|
|
viewportMessage.setBottom(Math.floor(viewport.bottom));
|
|
|
|
|
|
|
|
const userMovesMessage = new UserMovesMessage();
|
|
|
|
userMovesMessage.setPosition(positionMessage);
|
|
|
|
userMovesMessage.setViewport(viewportMessage);
|
|
|
|
|
2020-09-24 10:05:16 +02:00
|
|
|
//console.log('Sending position ', positionMessage.getX(), positionMessage.getY());
|
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
this.socket.emit(EventMessage.USER_POSITION, userMovesMessage.serializeBinary().buffer);
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 14:03:40 +02:00
|
|
|
public setSilent(silent: boolean): void {
|
|
|
|
this.socket.emit(EventMessage.SET_SILENT, silent);
|
|
|
|
}
|
|
|
|
|
2020-09-15 16:21:41 +02:00
|
|
|
public setViewport(viewport: ViewportInterface): void {
|
2020-09-24 17:36:10 +02:00
|
|
|
const viewportMessage = new ViewportMessage();
|
|
|
|
viewportMessage.setTop(Math.round(viewport.top));
|
|
|
|
viewportMessage.setBottom(Math.round(viewport.bottom));
|
|
|
|
viewportMessage.setLeft(Math.round(viewport.left));
|
|
|
|
viewportMessage.setRight(Math.round(viewport.right));
|
|
|
|
|
|
|
|
this.socket.emit(EventMessage.SET_VIEWPORT, viewportMessage.serializeBinary().buffer);
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
public onUserJoins(callback: (message: MessageUserJoined) => void): void {
|
2020-09-24 14:50:28 +02:00
|
|
|
this.onBatchMessage(EventMessage.JOIN_ROOM, (message: UserJoinedMessage) => {
|
|
|
|
const position = message.getPosition();
|
|
|
|
if (position === undefined) {
|
|
|
|
throw new Error('Invalid JOIN_ROOM message');
|
|
|
|
}
|
|
|
|
const messageUserJoined: MessageUserJoined = {
|
|
|
|
userId: message.getUserid(),
|
|
|
|
name: message.getName(),
|
|
|
|
characterLayers: message.getCharacterlayersList(),
|
|
|
|
position: ProtobufClientUtils.toPointInterface(position)
|
|
|
|
}
|
|
|
|
callback(messageUserJoined);
|
|
|
|
});
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 15:51:15 +02:00
|
|
|
public onUserMoved(callback: (message: UserMovedMessage) => void): void {
|
|
|
|
this.onBatchMessage(EventMessage.USER_MOVED, callback);
|
|
|
|
//this.socket.on(EventMessage.USER_MOVED, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a listener on a message that is part of a batch
|
|
|
|
*/
|
|
|
|
private onBatchMessage(eventName: string, callback: Function): void {
|
|
|
|
let callbacks = this.batchCallbacks.get(eventName);
|
|
|
|
if (callbacks === undefined) {
|
|
|
|
callbacks = new Array<Function>();
|
|
|
|
this.batchCallbacks.set(eventName, callbacks);
|
|
|
|
}
|
|
|
|
callbacks.push(callback);
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
public onUserLeft(callback: (userId: number) => void): void {
|
2020-09-24 16:11:47 +02:00
|
|
|
this.onBatchMessage(EventMessage.USER_LEFT, (message: UserLeftMessage) => {
|
|
|
|
callback(message.getUserid());
|
|
|
|
});
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
public onGroupUpdatedOrCreated(callback: (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => void): void {
|
2020-09-24 10:05:16 +02:00
|
|
|
this.onBatchMessage(EventMessage.GROUP_CREATE_UPDATE, (message: GroupUpdateMessage) => {
|
2020-09-21 11:24:03 +02:00
|
|
|
const position = message.getPosition();
|
|
|
|
if (position === undefined) {
|
|
|
|
throw new Error('Missing position in GROUP_CREATE_UPDATE');
|
|
|
|
}
|
|
|
|
|
|
|
|
const groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface = {
|
|
|
|
groupId: message.getGroupid(),
|
|
|
|
position: position.toObject()
|
|
|
|
}
|
|
|
|
|
2020-09-24 10:05:16 +02:00
|
|
|
//console.log('Group position: ', position.toObject());
|
2020-09-21 11:24:03 +02:00
|
|
|
callback(groupCreateUpdateMessage);
|
|
|
|
});
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|
|
|
|
|
2020-09-21 11:24:03 +02:00
|
|
|
public onGroupDeleted(callback: (groupId: number) => void): void {
|
2020-09-24 10:05:16 +02:00
|
|
|
this.onBatchMessage(EventMessage.GROUP_DELETE, (message: GroupDeleteMessage) => {
|
|
|
|
callback(message.getGroupid());
|
|
|
|
});
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 16:10:18 +02:00
|
|
|
public onConnectError(callback: (error: object) => void): void {
|
|
|
|
this.socket.on(EventMessage.CONNECT_ERROR, callback)
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
public sendWebrtcSignal(signal: unknown, receiverId: number) {
|
2020-06-22 15:00:23 +02:00
|
|
|
return this.socket.emit(EventMessage.WEBRTC_SIGNAL, {
|
2020-08-20 16:56:10 +02:00
|
|
|
receiverId: receiverId,
|
2020-04-25 16:05:33 +02:00
|
|
|
signal: signal
|
2020-08-20 16:56:10 +02:00
|
|
|
} as WebRtcSignalSentMessageInterface);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
public sendWebrtcScreenSharingSignal(signal: unknown, receiverId: number) {
|
2020-08-18 00:12:38 +02:00
|
|
|
return this.socket.emit(EventMessage.WEBRTC_SCREEN_SHARING_SIGNAL, {
|
2020-08-20 16:56:10 +02:00
|
|
|
receiverId: receiverId,
|
2020-06-11 23:18:06 +02:00
|
|
|
signal: signal
|
2020-08-20 16:56:10 +02:00
|
|
|
} as WebRtcSignalSentMessageInterface);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 18:42:54 +02:00
|
|
|
public receiveWebrtcStart(callback: (message: WebRtcStartMessageInterface) => void) {
|
2020-06-22 15:00:23 +02:00
|
|
|
this.socket.on(EventMessage.WEBRTC_START, callback);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-20 16:56:10 +02:00
|
|
|
public receiveWebrtcSignal(callback: (message: WebRtcSignalReceivedMessageInterface) => void) {
|
2020-06-22 15:00:23 +02:00
|
|
|
return this.socket.on(EventMessage.WEBRTC_SIGNAL, callback);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-20 16:56:10 +02:00
|
|
|
public receiveWebrtcScreenSharingSignal(callback: (message: WebRtcSignalReceivedMessageInterface) => void) {
|
2020-08-18 00:12:38 +02:00
|
|
|
return this.socket.on(EventMessage.WEBRTC_SCREEN_SHARING_SIGNAL, callback);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 15:00:23 +02:00
|
|
|
public onServerDisconnected(callback: (reason: string) => void): void {
|
|
|
|
this.socket.on('disconnect', (reason: string) => {
|
|
|
|
if (reason === 'io client disconnect') {
|
|
|
|
// The client asks for disconnect, let's not trigger any event.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback(reason);
|
2020-05-13 20:22:42 +02:00
|
|
|
});
|
|
|
|
|
2020-06-22 18:42:54 +02:00
|
|
|
}
|
2020-06-17 15:37:02 +02:00
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
public getUserId(): number|null {
|
2020-06-22 18:42:54 +02:00
|
|
|
return this.userId;
|
2020-05-13 20:22:42 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 13:07:18 +02:00
|
|
|
disconnectMessage(callback: (message: WebRtcDisconnectMessageInterface) => void): void {
|
2020-06-22 15:00:23 +02:00
|
|
|
this.socket.on(EventMessage.WEBRTC_DISCONNECT, callback);
|
2020-05-02 20:46:02 +02:00
|
|
|
}
|
2020-07-27 22:36:07 +02:00
|
|
|
|
2020-09-24 17:24:37 +02:00
|
|
|
emitActionableEvent(itemId: number, event: string, state: unknown, parameters: unknown): void {
|
|
|
|
const itemEventMessage = new ItemEventMessage();
|
|
|
|
itemEventMessage.setItemid(itemId);
|
|
|
|
itemEventMessage.setEvent(event);
|
|
|
|
itemEventMessage.setStatejson(JSON.stringify(state));
|
|
|
|
itemEventMessage.setParametersjson(JSON.stringify(parameters));
|
|
|
|
|
|
|
|
this.socket.emit(EventMessage.ITEM_EVENT, itemEventMessage.serializeBinary().buffer);
|
2020-07-27 22:36:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onActionableEvent(callback: (message: ItemEventMessageInterface) => void): void {
|
2020-09-24 17:24:37 +02:00
|
|
|
this.onBatchMessage(EventMessage.ITEM_EVENT, (message: ItemEventMessage) => {
|
|
|
|
callback({
|
|
|
|
itemId: message.getItemid(),
|
|
|
|
event: message.getEvent(),
|
|
|
|
parameters: JSON.parse(message.getParametersjson()),
|
|
|
|
state: JSON.parse(message.getStatejson())
|
|
|
|
});
|
|
|
|
});
|
2020-07-27 22:36:07 +02:00
|
|
|
}
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|