2020-04-05 20:57:14 +02:00
|
|
|
import Axios from "axios";
|
2020-05-10 17:31:27 +02:00
|
|
|
import {API_URL} from "./Enum/EnvironmentVariable";
|
2020-05-13 20:22:42 +02:00
|
|
|
import {MessageUI} from "./Logger/MessageUI";
|
2020-09-18 13:57:38 +02:00
|
|
|
import {
|
2020-09-29 16:01:22 +02:00
|
|
|
BatchMessage,
|
|
|
|
ClientToServerMessage,
|
|
|
|
GroupDeleteMessage,
|
|
|
|
GroupUpdateMessage,
|
|
|
|
ItemEventMessage,
|
|
|
|
JoinRoomMessage,
|
|
|
|
PositionMessage,
|
|
|
|
RoomJoinedMessage,
|
|
|
|
ServerToClientMessage,
|
|
|
|
SetPlayerDetailsMessage,
|
|
|
|
SetUserIdMessage,
|
|
|
|
SilentMessage,
|
|
|
|
UserJoinedMessage,
|
|
|
|
UserLeftMessage,
|
|
|
|
UserMovedMessage,
|
2020-09-18 13:57:38 +02:00
|
|
|
UserMovesMessage,
|
2020-09-29 16:01:22 +02:00
|
|
|
ViewportMessage,
|
|
|
|
WebRtcDisconnectMessage,
|
|
|
|
WebRtcSignalToClientMessage,
|
|
|
|
WebRtcSignalToServerMessage,
|
|
|
|
WebRtcStartMessage
|
2020-09-24 11:16:08 +02:00
|
|
|
} from "./Messages/generated/messages_pb"
|
2020-05-15 22:40:06 +02:00
|
|
|
|
2020-05-22 22:59:43 +02:00
|
|
|
import {PlayerAnimationNames} from "./Phaser/Player/Animation";
|
2020-06-08 09:20:36 +02:00
|
|
|
import {UserSimplePeerInterface} from "./WebRtc/SimplePeer";
|
2020-06-19 16:36:40 +02:00
|
|
|
import {SignalData} from "simple-peer";
|
2020-09-18 13:57:38 +02:00
|
|
|
import Direction = PositionMessage.Direction;
|
2020-09-24 14:50:28 +02:00
|
|
|
import {ProtobufClientUtils} from "./Network/ProtobufClientUtils";
|
2020-05-15 22:40:06 +02:00
|
|
|
|
2020-04-25 16:05:33 +02:00
|
|
|
enum EventMessage{
|
|
|
|
WEBRTC_SIGNAL = "webrtc-signal",
|
2020-06-11 23:18:06 +02:00
|
|
|
WEBRTC_SCREEN_SHARING_SIGNAL = "webrtc-screen-sharing-signal",
|
2020-04-25 16:05:33 +02:00
|
|
|
WEBRTC_START = "webrtc-start",
|
2020-05-19 19:11:12 +02:00
|
|
|
JOIN_ROOM = "join-room", // bi-directional
|
2020-09-15 10:06:11 +02:00
|
|
|
USER_POSITION = "user-position", // From client to server
|
2020-05-19 19:11:12 +02:00
|
|
|
USER_MOVED = "user-moved", // From server to client
|
|
|
|
USER_LEFT = "user-left", // From server to client
|
2020-05-02 20:46:02 +02:00
|
|
|
MESSAGE_ERROR = "message-error",
|
2020-05-08 00:35:36 +02:00
|
|
|
WEBRTC_DISCONNECT = "webrtc-disconect",
|
|
|
|
GROUP_CREATE_UPDATE = "group-create-update",
|
|
|
|
GROUP_DELETE = "group-delete",
|
2020-06-17 15:37:02 +02:00
|
|
|
SET_PLAYER_DETAILS = "set-player-details", // Send the name and character to the server (on connect), receive back the id.
|
2020-07-27 22:36:07 +02:00
|
|
|
ITEM_EVENT = 'item-event',
|
2020-05-13 20:22:42 +02:00
|
|
|
|
|
|
|
CONNECT_ERROR = "connect_error",
|
2020-08-31 14:03:40 +02:00
|
|
|
SET_SILENT = "set_silent", // Set or unset the silent mode for this user.
|
2020-09-15 10:06:11 +02:00
|
|
|
SET_VIEWPORT = "set-viewport",
|
|
|
|
BATCH = "batch",
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface PointInterface {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
direction : string;
|
2020-05-22 22:59:43 +02:00
|
|
|
moving: boolean;
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
export class Point implements PointInterface{
|
2020-05-22 22:59:43 +02:00
|
|
|
constructor(public x : number, public y : number, public direction : string = PlayerAnimationNames.WalkDown, public moving : boolean = false) {
|
2020-04-05 20:57:14 +02:00
|
|
|
if(x === null || y === null){
|
|
|
|
throw Error("position x and y cannot be null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface MessageUserPositionInterface {
|
2020-09-18 13:57:38 +02:00
|
|
|
userId: number;
|
2020-05-03 22:24:14 +02:00
|
|
|
name: string;
|
2020-07-28 17:43:33 +02:00
|
|
|
characterLayers: string[];
|
2020-04-10 12:54:05 +02:00
|
|
|
position: PointInterface;
|
|
|
|
}
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
export interface MessageUserMovedInterface {
|
2020-09-18 13:57:38 +02:00
|
|
|
userId: number;
|
2020-05-19 19:11:12 +02:00
|
|
|
position: PointInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MessageUserJoined {
|
2020-09-18 13:57:38 +02:00
|
|
|
userId: number;
|
2020-05-19 19:11:12 +02:00
|
|
|
name: string;
|
2020-07-28 17:43:33 +02:00
|
|
|
characterLayers: string[];
|
2020-05-22 22:59:43 +02:00
|
|
|
position: PointInterface
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
export interface PositionInterface {
|
|
|
|
x: number,
|
|
|
|
y: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface GroupCreatedUpdatedMessageInterface {
|
|
|
|
position: PositionInterface,
|
2020-09-21 11:24:03 +02:00
|
|
|
groupId: number
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 13:07:18 +02:00
|
|
|
export interface WebRtcStartMessageInterface {
|
|
|
|
roomId: string,
|
2020-06-08 09:20:36 +02:00
|
|
|
clients: UserSimplePeerInterface[]
|
2020-06-05 13:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface WebRtcDisconnectMessageInterface {
|
2020-09-18 13:57:38 +02:00
|
|
|
userId: number
|
2020-06-05 13:07:18 +02:00
|
|
|
}
|
|
|
|
|
2020-08-20 16:56:10 +02:00
|
|
|
export interface WebRtcSignalSentMessageInterface {
|
2020-09-18 13:57:38 +02:00
|
|
|
receiverId: number,
|
2020-06-19 16:36:40 +02:00
|
|
|
signal: SignalData
|
|
|
|
}
|
|
|
|
|
2020-08-20 16:56:10 +02:00
|
|
|
export interface WebRtcSignalReceivedMessageInterface {
|
2020-09-18 13:57:38 +02:00
|
|
|
userId: number,
|
2020-06-19 16:36:40 +02:00
|
|
|
signal: SignalData
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface StartMapInterface {
|
|
|
|
mapUrlStart: string,
|
|
|
|
startInstance: string
|
|
|
|
}
|
|
|
|
|
2020-09-15 10:06:11 +02:00
|
|
|
export interface ViewportInterface {
|
|
|
|
left: number,
|
|
|
|
top: number,
|
|
|
|
right: number,
|
|
|
|
bottom: number,
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface BatchedMessageInterface {
|
|
|
|
event: string,
|
2020-09-15 10:10:35 +02:00
|
|
|
payload: unknown
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 22:36:07 +02:00
|
|
|
export interface ItemEventMessageInterface {
|
|
|
|
itemId: number,
|
|
|
|
event: string,
|
|
|
|
state: unknown,
|
|
|
|
parameters: unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RoomJoinedMessageInterface {
|
2020-09-16 16:06:43 +02:00
|
|
|
users: MessageUserPositionInterface[],
|
|
|
|
groups: GroupCreatedUpdatedMessageInterface[],
|
2020-07-27 22:36:07 +02:00
|
|
|
items: { [itemId: number] : unknown }
|
|
|
|
}
|
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
export class Connection implements Connection {
|
2020-09-28 18:52:54 +02:00
|
|
|
private readonly socket: WebSocket;
|
2020-09-18 13:57:38 +02:00
|
|
|
private userId: number|null = null;
|
2020-09-29 16:01:22 +02:00
|
|
|
private listeners: Map<string, Function[]> = new Map<string, Function[]>();
|
2020-09-28 18:52:54 +02:00
|
|
|
private static websocketFactory: null|((url: string)=>any) = null;
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
public static setWebsocketFactory(websocketFactory: (url: string)=>any): void {
|
|
|
|
Connection.websocketFactory = websocketFactory;
|
|
|
|
}
|
2020-06-22 15:00:23 +02:00
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
private constructor(token: string) {
|
|
|
|
let url = API_URL.replace('http://', 'ws://').replace('https://', 'wss://');
|
|
|
|
url += '?token='+token;
|
2020-09-15 10:06:11 +02:00
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
if (Connection.websocketFactory) {
|
|
|
|
this.socket = Connection.websocketFactory(url);
|
|
|
|
} else {
|
|
|
|
this.socket = new WebSocket(url);
|
|
|
|
}
|
2020-09-18 15:51:15 +02:00
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
this.socket.binaryType = 'arraybuffer';
|
|
|
|
|
|
|
|
this.socket.onopen = (ev) => {
|
|
|
|
console.log('WS connected');
|
|
|
|
};
|
|
|
|
|
|
|
|
this.socket.onmessage = (messageEvent) => {
|
|
|
|
const arrayBuffer: ArrayBuffer = messageEvent.data;
|
|
|
|
const message = ServerToClientMessage.deserializeBinary(new Uint8Array(arrayBuffer));
|
|
|
|
|
|
|
|
if (message.hasBatchmessage()) {
|
|
|
|
for (const subMessage of (message.getBatchmessage() as BatchMessage).getPayloadList()) {
|
|
|
|
let event: string;
|
|
|
|
let payload;
|
|
|
|
if (subMessage.hasUsermovedmessage()) {
|
|
|
|
event = EventMessage.USER_MOVED;
|
|
|
|
payload = subMessage.getUsermovedmessage();
|
|
|
|
} else if (subMessage.hasGroupupdatemessage()) {
|
|
|
|
event = EventMessage.GROUP_CREATE_UPDATE;
|
|
|
|
payload = subMessage.getGroupupdatemessage();
|
|
|
|
} else if (subMessage.hasGroupdeletemessage()) {
|
|
|
|
event = EventMessage.GROUP_DELETE;
|
|
|
|
payload = subMessage.getGroupdeletemessage();
|
|
|
|
} else if (subMessage.hasUserjoinedmessage()) {
|
|
|
|
event = EventMessage.JOIN_ROOM;
|
|
|
|
payload = subMessage.getUserjoinedmessage();
|
|
|
|
} else if (subMessage.hasUserleftmessage()) {
|
|
|
|
event = EventMessage.USER_LEFT;
|
|
|
|
payload = subMessage.getUserleftmessage();
|
|
|
|
} else if (subMessage.hasItemeventmessage()) {
|
|
|
|
event = EventMessage.ITEM_EVENT;
|
|
|
|
payload = subMessage.getItemeventmessage();
|
|
|
|
} else {
|
|
|
|
throw new Error('Unexpected batch message type');
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:01:22 +02:00
|
|
|
this.dispatch(event, payload);
|
2020-09-18 15:51:15 +02:00
|
|
|
}
|
2020-09-28 18:52:54 +02:00
|
|
|
} else if (message.hasRoomjoinedmessage()) {
|
|
|
|
const roomJoinedMessage = message.getRoomjoinedmessage() as RoomJoinedMessage;
|
|
|
|
|
|
|
|
const users: Array<MessageUserJoined> = roomJoinedMessage.getUserList().map(this.toMessageUserJoined);
|
|
|
|
const groups: Array<GroupCreatedUpdatedMessageInterface> = roomJoinedMessage.getGroupList().map(this.toGroupCreatedUpdatedMessage);
|
|
|
|
let items: { [itemId: number] : unknown } = {};
|
|
|
|
for (const item of roomJoinedMessage.getItemList()) {
|
|
|
|
items[item.getItemid()] = JSON.parse(item.getStatejson());
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
2020-09-28 18:52:54 +02:00
|
|
|
|
|
|
|
this.resolveJoinRoom({
|
|
|
|
users,
|
|
|
|
groups,
|
|
|
|
items
|
|
|
|
})
|
|
|
|
} else if (message.hasSetuseridmessage()) {
|
|
|
|
this.userId = (message.getSetuseridmessage() as SetUserIdMessage).getUserid();
|
|
|
|
} else if (message.hasErrormessage()) {
|
|
|
|
console.error(EventMessage.MESSAGE_ERROR, message.getErrormessage()?.getMessage);
|
2020-09-29 16:01:22 +02:00
|
|
|
} else if (message.hasWebrtcsignaltoclientmessage()) {
|
|
|
|
this.dispatch(EventMessage.WEBRTC_SIGNAL, message.getWebrtcsignaltoclientmessage());
|
|
|
|
} else if (message.hasWebrtcscreensharingsignaltoclientmessage()) {
|
|
|
|
this.dispatch(EventMessage.WEBRTC_SCREEN_SHARING_SIGNAL, message.getWebrtcscreensharingsignaltoclientmessage());
|
|
|
|
} else if (message.hasWebrtcstartmessage()) {
|
|
|
|
console.log('Received WebRtcStartMessage');
|
|
|
|
this.dispatch(EventMessage.WEBRTC_START, message.getWebrtcstartmessage());
|
|
|
|
} else if (message.hasWebrtcdisconnectmessage()) {
|
|
|
|
this.dispatch(EventMessage.WEBRTC_DISCONNECT, message.getWebrtcdisconnectmessage());
|
|
|
|
} else {
|
|
|
|
throw new Error('Unknown message received');
|
2020-09-15 10:06:11 +02:00
|
|
|
}
|
2020-09-28 18:52:54 +02:00
|
|
|
|
|
|
|
}
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 16:01:22 +02:00
|
|
|
private dispatch(event: string, payload: unknown): void {
|
|
|
|
const listeners = this.listeners.get(event);
|
|
|
|
if (listeners === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const listener of listeners) {
|
|
|
|
listener(payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:43:33 +02:00
|
|
|
public static createConnection(name: string, characterLayersSelected: string[]): Promise<Connection> {
|
2020-05-23 14:00:36 +02:00
|
|
|
return Axios.post(`${API_URL}/login`, {name: name})
|
2020-04-05 20:57:14 +02:00
|
|
|
.then((res) => {
|
2020-06-22 11:58:07 +02:00
|
|
|
|
2020-06-22 16:10:18 +02:00
|
|
|
return new Promise<Connection>((resolve, reject) => {
|
2020-06-22 18:42:54 +02:00
|
|
|
const connection = new Connection(res.data.token);
|
2020-06-22 16:10:18 +02:00
|
|
|
|
|
|
|
connection.onConnectError((error: object) => {
|
|
|
|
console.log('An error occurred while connecting to socket server. Retrying');
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
connection.onConnect(() => {
|
|
|
|
const message = new SetPlayerDetailsMessage();
|
|
|
|
message.setName(name);
|
|
|
|
message.setCharacterlayersList(characterLayersSelected);
|
|
|
|
|
|
|
|
const clientToServerMessage = new ClientToServerMessage();
|
|
|
|
clientToServerMessage.setSetplayerdetailsmessage(message);
|
2020-06-22 16:10:18 +02:00
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
connection.socket.send(clientToServerMessage.serializeBinary().buffer);
|
|
|
|
|
|
|
|
resolve(connection);
|
|
|
|
});
|
2020-06-22 16:10:18 +02:00
|
|
|
});
|
2020-04-05 20:57:14 +02:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2020-06-22 15:00:23 +02:00
|
|
|
// Let's retry in 4-6 seconds
|
2020-09-28 18:52:54 +02:00
|
|
|
console.error('Connection failed. Retrying', err);
|
2020-06-22 15:00:23 +02:00
|
|
|
return new Promise<Connection>((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
2020-07-28 17:43:33 +02:00
|
|
|
Connection.createConnection(name, characterLayersSelected).then((connection) => resolve(connection))
|
2020-06-22 16:10:18 +02:00
|
|
|
.catch((error) => reject(error));
|
2020-06-22 15:00:23 +02:00
|
|
|
}, 4000 + Math.floor(Math.random() * 2000) );
|
|
|
|
});
|
2020-05-23 14:00:36 +02:00
|
|
|
});
|
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-09-28 18:52:54 +02:00
|
|
|
private resolveJoinRoom!: (value?: (RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface> | undefined)) => void;
|
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-28 18:52:54 +02:00
|
|
|
this.resolveJoinRoom = resolve;
|
|
|
|
|
|
|
|
const positionMessage = this.toPositionMessage(startX, startY, direction, moving);
|
|
|
|
const viewportMessage = this.toViewportMessage(viewport);
|
|
|
|
|
|
|
|
const joinRoomMessage = new JoinRoomMessage();
|
|
|
|
joinRoomMessage.setRoomid(roomId);
|
|
|
|
joinRoomMessage.setPosition(positionMessage);
|
|
|
|
joinRoomMessage.setViewport(viewportMessage);
|
|
|
|
|
|
|
|
//console.log('Sending position ', positionMessage.getX(), positionMessage.getY());
|
|
|
|
const clientToServerMessage = new ClientToServerMessage();
|
|
|
|
clientToServerMessage.setJoinroommessage(joinRoomMessage);
|
|
|
|
|
|
|
|
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
2020-06-22 11:58:07 +02:00
|
|
|
})
|
|
|
|
return promise;
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
private toPositionMessage(x : number, y : number, direction : string, moving: boolean): PositionMessage {
|
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);
|
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
return positionMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
private toViewportMessage(viewport: ViewportInterface): ViewportMessage {
|
2020-09-18 13:57:38 +02:00
|
|
|
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));
|
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
return viewportMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public sharePosition(x : number, y : number, direction : string, moving: boolean, viewport: ViewportInterface) : void{
|
|
|
|
if(!this.socket){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const positionMessage = this.toPositionMessage(x, y, direction, moving);
|
|
|
|
|
|
|
|
const viewportMessage = this.toViewportMessage(viewport);
|
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
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-28 18:52:54 +02:00
|
|
|
const clientToServerMessage = new ClientToServerMessage();
|
|
|
|
clientToServerMessage.setUsermovesmessage(userMovesMessage);
|
2020-09-24 10:05:16 +02:00
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 14:03:40 +02:00
|
|
|
public setSilent(silent: boolean): void {
|
2020-09-28 18:52:54 +02:00
|
|
|
const silentMessage = new SilentMessage();
|
|
|
|
silentMessage.setSilent(silent);
|
|
|
|
|
|
|
|
const clientToServerMessage = new ClientToServerMessage();
|
|
|
|
clientToServerMessage.setSilentmessage(silentMessage);
|
|
|
|
|
|
|
|
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
2020-08-31 14:03:40 +02:00
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
const clientToServerMessage = new ClientToServerMessage();
|
|
|
|
clientToServerMessage.setViewportmessage(viewportMessage);
|
|
|
|
|
|
|
|
this.socket.send(clientToServerMessage.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-29 16:01:22 +02:00
|
|
|
this.onMessage(EventMessage.JOIN_ROOM, (message: UserJoinedMessage) => {
|
2020-09-28 18:52:54 +02:00
|
|
|
callback(this.toMessageUserJoined(message));
|
2020-09-24 14:50:28 +02:00
|
|
|
});
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
// TODO: move this to protobuf utils
|
|
|
|
private toMessageUserJoined(message: UserJoinedMessage): MessageUserJoined {
|
|
|
|
const position = message.getPosition();
|
|
|
|
if (position === undefined) {
|
|
|
|
throw new Error('Invalid JOIN_ROOM message');
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
userId: message.getUserid(),
|
|
|
|
name: message.getName(),
|
|
|
|
characterLayers: message.getCharacterlayersList(),
|
|
|
|
position: ProtobufClientUtils.toPointInterface(position)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 15:51:15 +02:00
|
|
|
public onUserMoved(callback: (message: UserMovedMessage) => void): void {
|
2020-09-29 16:01:22 +02:00
|
|
|
this.onMessage(EventMessage.USER_MOVED, callback);
|
2020-09-18 15:51:15 +02:00
|
|
|
//this.socket.on(EventMessage.USER_MOVED, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a listener on a message that is part of a batch
|
|
|
|
*/
|
2020-09-29 16:01:22 +02:00
|
|
|
private onMessage(eventName: string, callback: Function): void {
|
|
|
|
let callbacks = this.listeners.get(eventName);
|
2020-09-18 15:51:15 +02:00
|
|
|
if (callbacks === undefined) {
|
|
|
|
callbacks = new Array<Function>();
|
2020-09-29 16:01:22 +02:00
|
|
|
this.listeners.set(eventName, callbacks);
|
2020-09-18 15:51:15 +02:00
|
|
|
}
|
|
|
|
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-29 16:01:22 +02:00
|
|
|
this.onMessage(EventMessage.USER_LEFT, (message: UserLeftMessage) => {
|
2020-09-24 16:11:47 +02:00
|
|
|
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-29 16:01:22 +02:00
|
|
|
this.onMessage(EventMessage.GROUP_CREATE_UPDATE, (message: GroupUpdateMessage) => {
|
2020-09-28 18:52:54 +02:00
|
|
|
callback(this.toGroupCreatedUpdatedMessage(message));
|
|
|
|
});
|
|
|
|
}
|
2020-09-21 11:24:03 +02:00
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
private toGroupCreatedUpdatedMessage(message: GroupUpdateMessage): GroupCreatedUpdatedMessageInterface {
|
|
|
|
const position = message.getPosition();
|
|
|
|
if (position === undefined) {
|
|
|
|
throw new Error('Missing position in GROUP_CREATE_UPDATE');
|
|
|
|
}
|
2020-09-21 11:24:03 +02:00
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
return {
|
|
|
|
groupId: message.getGroupid(),
|
|
|
|
position: position.toObject()
|
|
|
|
}
|
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-29 16:01:22 +02:00
|
|
|
this.onMessage(EventMessage.GROUP_DELETE, (message: GroupDeleteMessage) => {
|
2020-09-24 10:05:16 +02:00
|
|
|
callback(message.getGroupid());
|
|
|
|
});
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
public onConnectError(callback: (error: Event) => void): void {
|
|
|
|
this.socket.addEventListener('error', callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
public onConnect(callback: (event: Event) => void): void {
|
|
|
|
this.socket.addEventListener('open', callback)
|
2020-06-22 16:10:18 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
public sendWebrtcSignal(signal: unknown, receiverId: number) {
|
2020-09-29 16:01:22 +02:00
|
|
|
const webRtcSignal = new WebRtcSignalToServerMessage();
|
|
|
|
webRtcSignal.setReceiverid(receiverId);
|
|
|
|
webRtcSignal.setSignal(JSON.stringify(signal));
|
|
|
|
|
|
|
|
const clientToServerMessage = new ClientToServerMessage();
|
|
|
|
clientToServerMessage.setWebrtcsignaltoservermessage(webRtcSignal);
|
|
|
|
|
|
|
|
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
public sendWebrtcScreenSharingSignal(signal: unknown, receiverId: number) {
|
2020-09-29 16:01:22 +02:00
|
|
|
const webRtcSignal = new WebRtcSignalToServerMessage();
|
|
|
|
webRtcSignal.setReceiverid(receiverId);
|
|
|
|
webRtcSignal.setSignal(JSON.stringify(signal));
|
|
|
|
|
|
|
|
const clientToServerMessage = new ClientToServerMessage();
|
|
|
|
clientToServerMessage.setWebrtcscreensharingsignaltoservermessage(webRtcSignal);
|
|
|
|
|
|
|
|
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 16:01:22 +02:00
|
|
|
public receiveWebrtcStart(callback: (message: UserSimplePeerInterface) => void) {
|
|
|
|
this.onMessage(EventMessage.WEBRTC_START, (message: WebRtcStartMessage) => {
|
|
|
|
callback({
|
|
|
|
userId: message.getUserid(),
|
|
|
|
name: message.getName(),
|
|
|
|
initiator: message.getInitiator()
|
|
|
|
});
|
|
|
|
});
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-20 16:56:10 +02:00
|
|
|
public receiveWebrtcSignal(callback: (message: WebRtcSignalReceivedMessageInterface) => void) {
|
2020-09-29 16:01:22 +02:00
|
|
|
this.onMessage(EventMessage.WEBRTC_SIGNAL, (message: WebRtcSignalToClientMessage) => {
|
|
|
|
callback({
|
|
|
|
userId: message.getUserid(),
|
|
|
|
signal: JSON.parse(message.getSignal())
|
|
|
|
});
|
|
|
|
});
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-20 16:56:10 +02:00
|
|
|
public receiveWebrtcScreenSharingSignal(callback: (message: WebRtcSignalReceivedMessageInterface) => void) {
|
2020-09-29 16:01:22 +02:00
|
|
|
this.onMessage(EventMessage.WEBRTC_SCREEN_SHARING_SIGNAL, (message: WebRtcSignalToClientMessage) => {
|
|
|
|
callback({
|
|
|
|
userId: message.getUserid(),
|
|
|
|
signal: JSON.parse(message.getSignal())
|
|
|
|
});
|
|
|
|
});
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
public onServerDisconnected(callback: (event: CloseEvent) => void): void {
|
|
|
|
this.socket.addEventListener('close', (event) => {
|
2020-09-29 16:01:22 +02:00
|
|
|
console.log('Socket closed with code '+event.code+". Reason: "+event.reason);
|
2020-09-28 18:52:54 +02:00
|
|
|
if (event.code === 1000) {
|
|
|
|
// Normal closure case
|
2020-06-22 15:00:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-09-28 18:52:54 +02:00
|
|
|
callback(event);
|
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-09-29 16:01:22 +02:00
|
|
|
this.onMessage(EventMessage.WEBRTC_DISCONNECT, (message: WebRtcDisconnectMessage) => {
|
|
|
|
callback({
|
|
|
|
userId: message.getUserid()
|
|
|
|
});
|
|
|
|
});
|
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));
|
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
const clientToServerMessage = new ClientToServerMessage();
|
|
|
|
clientToServerMessage.setItemeventmessage(itemEventMessage);
|
|
|
|
|
|
|
|
this.socket.send(clientToServerMessage.serializeBinary().buffer);
|
2020-07-27 22:36:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onActionableEvent(callback: (message: ItemEventMessageInterface) => void): void {
|
2020-09-29 16:01:22 +02:00
|
|
|
this.onMessage(EventMessage.ITEM_EVENT, (message: ItemEventMessage) => {
|
2020-09-24 17:24:37 +02:00
|
|
|
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
|
|
|
}
|