Switching WebRTC to protobuf + uws
This commit is contained in:
parent
a9b1313d39
commit
b485c9bf46
@ -37,7 +37,12 @@ import {
|
||||
ErrorMessage,
|
||||
RoomJoinedMessage,
|
||||
ItemStateMessage,
|
||||
ServerToClientMessage, SetUserIdMessage, SilentMessage
|
||||
ServerToClientMessage,
|
||||
SetUserIdMessage,
|
||||
SilentMessage,
|
||||
WebRtcSignalToClientMessage,
|
||||
WebRtcSignalToServerMessage,
|
||||
WebRtcStartMessage, WebRtcDisconnectMessage
|
||||
} from "../Messages/generated/messages_pb";
|
||||
import {UserMovesMessage} from "../Messages/generated/messages_pb";
|
||||
import Direction = PositionMessage.Direction;
|
||||
@ -186,7 +191,7 @@ export class IoSocketController {
|
||||
/* Options */
|
||||
//compression: uWS.SHARED_COMPRESSOR,
|
||||
maxPayloadLength: 16 * 1024 * 1024,
|
||||
idleTimeout: 10,
|
||||
//idleTimeout: 10,
|
||||
/* Handlers */
|
||||
open: (ws) => {
|
||||
this.authenticate(ws);
|
||||
@ -222,6 +227,10 @@ export class IoSocketController {
|
||||
this.handleSilentMessage(client, message.getSilentmessage() as SilentMessage);
|
||||
} else if (message.hasItemeventmessage()) {
|
||||
this.handleItemEvent(client, message.getItemeventmessage() as ItemEventMessage);
|
||||
} else if (message.hasWebrtcsignaltoservermessage()) {
|
||||
this.emitVideo(client, message.getWebrtcsignaltoservermessage() as WebRtcSignalToServerMessage)
|
||||
} else if (message.hasWebrtcscreensharingsignaltoservermessage()) {
|
||||
this.emitScreenSharing(client, message.getWebrtcscreensharingsignaltoservermessage() as WebRtcSignalToServerMessage)
|
||||
}
|
||||
|
||||
/* Ok is false if backpressure was built up, wait for drain */
|
||||
@ -282,7 +291,7 @@ export class IoSocketController {
|
||||
serverToClientMessage.setErrormessage(errorMessage);
|
||||
|
||||
if (!Client.disconnecting) {
|
||||
Client.send(serverToClientMessage.serializeBinary().buffer);
|
||||
Client.send(serverToClientMessage.serializeBinary().buffer, true);
|
||||
}
|
||||
console.warn(message);
|
||||
}
|
||||
@ -496,40 +505,44 @@ export class IoSocketController {
|
||||
}
|
||||
}
|
||||
|
||||
emitVideo(socket: ExSocketInterface, data: unknown){
|
||||
if (!isWebRtcSignalMessageInterface(data)) {
|
||||
socket.emit(SocketIoEvent.MESSAGE_ERROR, {message: 'Invalid WEBRTC_SIGNAL message.'});
|
||||
console.warn('Invalid WEBRTC_SIGNAL message received: ', data);
|
||||
return;
|
||||
}
|
||||
emitVideo(socket: ExSocketInterface, data: WebRtcSignalToServerMessage): void {
|
||||
//send only at user
|
||||
const client = this.sockets.get(data.receiverId);
|
||||
const client = this.sockets.get(data.getReceiverid());
|
||||
if (client === undefined) {
|
||||
console.warn("While exchanging a WebRTC signal: client with id ", data.receiverId, " does not exist. This might be a race condition.");
|
||||
console.warn("While exchanging a WebRTC signal: client with id ", data.getReceiverid(), " does not exist. This might be a race condition.");
|
||||
return;
|
||||
}
|
||||
return client.emit(SocketIoEvent.WEBRTC_SIGNAL, {
|
||||
userId: socket.userId,
|
||||
signal: data.signal
|
||||
});
|
||||
|
||||
const webrtcSignalToClient = new WebRtcSignalToClientMessage();
|
||||
webrtcSignalToClient.setUserid(socket.userId);
|
||||
webrtcSignalToClient.setSignal(data.getSignal());
|
||||
|
||||
const serverToClientMessage = new ServerToClientMessage();
|
||||
serverToClientMessage.setWebrtcsignaltoclientmessage(webrtcSignalToClient);
|
||||
|
||||
if (!client.disconnecting) {
|
||||
client.send(serverToClientMessage.serializeBinary().buffer, true);
|
||||
}
|
||||
}
|
||||
|
||||
emitScreenSharing(socket: ExSocketInterface, data: unknown){
|
||||
if (!isWebRtcSignalMessageInterface(data)) {
|
||||
socket.emit(SocketIoEvent.MESSAGE_ERROR, {message: 'Invalid WEBRTC_SCREEN_SHARING message.'});
|
||||
console.warn('Invalid WEBRTC_SCREEN_SHARING message received: ', data);
|
||||
return;
|
||||
}
|
||||
emitScreenSharing(socket: ExSocketInterface, data: WebRtcSignalToServerMessage): void {
|
||||
//send only at user
|
||||
const client = this.sockets.get(data.receiverId);
|
||||
const client = this.sockets.get(data.getReceiverid());
|
||||
if (client === undefined) {
|
||||
console.warn("While exchanging a WEBRTC_SCREEN_SHARING signal: client with id ", data.receiverId, " does not exist. This might be a race condition.");
|
||||
console.warn("While exchanging a WEBRTC_SCREEN_SHARING signal: client with id ", data.getReceiverid(), " does not exist. This might be a race condition.");
|
||||
return;
|
||||
}
|
||||
return client.emit(SocketIoEvent.WEBRTC_SCREEN_SHARING_SIGNAL, {
|
||||
userId: socket.userId,
|
||||
signal: data.signal
|
||||
});
|
||||
|
||||
const webrtcSignalToClient = new WebRtcSignalToClientMessage();
|
||||
webrtcSignalToClient.setUserid(socket.userId);
|
||||
webrtcSignalToClient.setSignal(data.getSignal());
|
||||
|
||||
const serverToClientMessage = new ServerToClientMessage();
|
||||
serverToClientMessage.setWebrtcscreensharingsignaltoclientmessage(webrtcSignalToClient);
|
||||
|
||||
if (!client.disconnecting) {
|
||||
client.send(serverToClientMessage.serializeBinary().buffer, true);
|
||||
}
|
||||
}
|
||||
|
||||
searchClientByIdOrFail(userId: number): ExSocketInterface {
|
||||
@ -571,9 +584,9 @@ export class IoSocketController {
|
||||
//check and create new world for a room
|
||||
let world = this.Worlds.get(roomId)
|
||||
if(world === undefined){
|
||||
world = new World((user1: number, group: Group) => {
|
||||
this.connectedUser(user1, group);
|
||||
}, (user1: number, group: Group) => {
|
||||
world = new World((user1: User, group: Group) => {
|
||||
this.joinWebRtcRoom(user1, group);
|
||||
}, (user1: User, group: Group) => {
|
||||
this.disConnectedUser(user1, group);
|
||||
}, MINIMUM_DISTANCE, GROUP_RADIUS, (thing: Movable, listener: User) => {
|
||||
const clientListener = this.searchClientByIdOrFail(listener.id);
|
||||
@ -677,20 +690,49 @@ export class IoSocketController {
|
||||
emitInBatch(client, subMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param socket
|
||||
* @param roomId
|
||||
*/
|
||||
joinWebRtcRoom(socket: ExSocketInterface, roomId: string) {
|
||||
|
||||
// TODO: REBUILD THIS
|
||||
return;
|
||||
|
||||
/* if (socket.webRtcRoomId === roomId) {
|
||||
joinWebRtcRoom(user: User, group: Group) {
|
||||
/*const roomId: string = "webrtcroom"+group.getId();
|
||||
if (user.socket.webRtcRoomId === roomId) {
|
||||
return;
|
||||
}*/
|
||||
|
||||
// TODO: joinWebRtcRoom will be trigerred twice when joining the first time! Maybe we should fix the GROUP constructor to trigger only one event
|
||||
console.log('joinWebRtcRoom FOR '+user.socket.name+" "+user.socket.userId);
|
||||
for (const otherUser of group.getUsers()) {
|
||||
if (user === otherUser) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Let's send 2 messages: one to the user joining the group and one to the other user
|
||||
const webrtcStartMessage1 = new WebRtcStartMessage();
|
||||
webrtcStartMessage1.setUserid(otherUser.id);
|
||||
webrtcStartMessage1.setName(otherUser.socket.name);
|
||||
webrtcStartMessage1.setInitiator(true);
|
||||
|
||||
const serverToClientMessage1 = new ServerToClientMessage();
|
||||
serverToClientMessage1.setWebrtcstartmessage(webrtcStartMessage1);
|
||||
|
||||
if (!user.socket.disconnecting) {
|
||||
user.socket.send(serverToClientMessage1.serializeBinary().buffer, true);
|
||||
console.log('Sending webrtcstart initiator to '+user.socket.userId)
|
||||
}
|
||||
|
||||
const webrtcStartMessage2 = new WebRtcStartMessage();
|
||||
webrtcStartMessage2.setUserid(user.id);
|
||||
webrtcStartMessage2.setName(user.socket.name);
|
||||
webrtcStartMessage2.setInitiator(false);
|
||||
|
||||
const serverToClientMessage2 = new ServerToClientMessage();
|
||||
serverToClientMessage2.setWebrtcstartmessage(webrtcStartMessage2);
|
||||
|
||||
if (!otherUser.socket.disconnecting) {
|
||||
otherUser.socket.send(serverToClientMessage2.serializeBinary().buffer, true);
|
||||
console.log('Sending webrtcstart to '+otherUser.socket.userId)
|
||||
}
|
||||
|
||||
}
|
||||
socket.join(roomId);
|
||||
|
||||
/* socket.join(roomId);
|
||||
socket.webRtcRoomId = roomId;
|
||||
//if two persons in room share
|
||||
if (this.Io.sockets.adapter.rooms[roomId].length < 2) {
|
||||
@ -737,43 +779,49 @@ export class IoSocketController {
|
||||
]
|
||||
**/
|
||||
|
||||
//connected user
|
||||
connectedUser(userId: number, group: Group) {
|
||||
/*let Client = this.sockets.get(userId);
|
||||
if (Client === undefined) {
|
||||
return;
|
||||
}*/
|
||||
const Client = this.searchClientByIdOrFail(userId);
|
||||
this.joinWebRtcRoom(Client, "webrtcroom"+group.getId());
|
||||
}
|
||||
|
||||
//disconnect user
|
||||
disConnectedUser(userId: number, group: Group) {
|
||||
// TODO: rebuild this
|
||||
return;
|
||||
disConnectedUser(user: User, group: Group) {
|
||||
|
||||
const Client = this.searchClientByIdOrFail(userId);
|
||||
Client.to("webrtcroom"+group.getId()).emit(SocketIoEvent.WEBRTC_DISCONNECT, {
|
||||
userId: userId
|
||||
});
|
||||
const Client = user.socket;
|
||||
|
||||
// Most of the time, sending a disconnect event to one of the players is enough (the player will close the connection
|
||||
// which will be shut for the other player).
|
||||
// However! In the rare case where the WebRTC connection is not yet established, if we close the connection on one of the player,
|
||||
// the other player will try connecting until a timeout happens (during this time, the connection icon will be displayed for nothing).
|
||||
// So we also send the disconnect event to the other player.
|
||||
for (const user of group.getUsers()) {
|
||||
Client.emit(SocketIoEvent.WEBRTC_DISCONNECT, {
|
||||
userId: user.id
|
||||
});
|
||||
for (const otherUser of group.getUsers()) {
|
||||
if (user === otherUser) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const webrtcDisconnectMessage1 = new WebRtcDisconnectMessage();
|
||||
webrtcDisconnectMessage1.setUserid(user.id);
|
||||
|
||||
const serverToClientMessage1 = new ServerToClientMessage();
|
||||
serverToClientMessage1.setWebrtcdisconnectmessage(webrtcDisconnectMessage1);
|
||||
|
||||
if (!otherUser.socket.disconnecting) {
|
||||
otherUser.socket.send(serverToClientMessage1.serializeBinary().buffer, true);
|
||||
}
|
||||
|
||||
|
||||
const webrtcDisconnectMessage2 = new WebRtcDisconnectMessage();
|
||||
webrtcDisconnectMessage2.setUserid(otherUser.id);
|
||||
|
||||
const serverToClientMessage2 = new ServerToClientMessage();
|
||||
serverToClientMessage2.setWebrtcdisconnectmessage(webrtcDisconnectMessage2);
|
||||
|
||||
if (!user.socket.disconnecting) {
|
||||
user.socket.send(serverToClientMessage2.serializeBinary().buffer, true);
|
||||
}
|
||||
}
|
||||
|
||||
//disconnect webrtc room
|
||||
if(!Client.webRtcRoomId){
|
||||
/*if(!Client.webRtcRoomId){
|
||||
return;
|
||||
}
|
||||
Client.leave(Client.webRtcRoomId);
|
||||
delete Client.webRtcRoomId;
|
||||
}*/
|
||||
//Client.leave(Client.webRtcRoomId);
|
||||
//delete Client.webRtcRoomId;
|
||||
}
|
||||
|
||||
public getWorlds(): Map<string, World> {
|
||||
|
@ -87,7 +87,7 @@ export class Group implements Movable {
|
||||
join(user: User): void
|
||||
{
|
||||
// Broadcast on the right event
|
||||
this.connectCallback(user.id, this);
|
||||
this.connectCallback(user, this);
|
||||
this.users.add(user);
|
||||
user.group = this;
|
||||
}
|
||||
@ -105,7 +105,7 @@ export class Group implements Movable {
|
||||
}
|
||||
|
||||
// Broadcast on the right event
|
||||
this.disconnectCallback(user.id, this);
|
||||
this.disconnectCallback(user, this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4,6 +4,7 @@ import {Zone} from "_Model/Zone";
|
||||
import {Movable} from "_Model/Movable";
|
||||
import {PositionInterface} from "_Model/PositionInterface";
|
||||
import {PositionNotifier} from "_Model/PositionNotifier";
|
||||
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||
|
||||
export class User implements Movable {
|
||||
public listenedZones: Set<Zone>;
|
||||
@ -13,7 +14,8 @@ export class User implements Movable {
|
||||
public id: number,
|
||||
private position: PointInterface,
|
||||
public silent: boolean,
|
||||
private positionNotifier: PositionNotifier
|
||||
private positionNotifier: PositionNotifier,
|
||||
public readonly socket: ExSocketInterface
|
||||
) {
|
||||
this.listenedZones = new Set<Zone>();
|
||||
|
||||
|
@ -11,8 +11,8 @@ import {PositionNotifier} from "./PositionNotifier";
|
||||
import {ViewportInterface} from "_Model/Websocket/ViewportMessage";
|
||||
import {Movable} from "_Model/Movable";
|
||||
|
||||
export type ConnectCallback = (user: number, group: Group) => void;
|
||||
export type DisconnectCallback = (user: number, group: Group) => void;
|
||||
export type ConnectCallback = (user: User, group: Group) => void;
|
||||
export type DisconnectCallback = (user: User, group: Group) => void;
|
||||
|
||||
export class World {
|
||||
private readonly minDistance: number;
|
||||
@ -55,8 +55,8 @@ export class World {
|
||||
return this.users;
|
||||
}
|
||||
|
||||
public join(socket : Identificable, userPosition: PointInterface): void {
|
||||
const user = new User(socket.userId, userPosition, false, this.positionNotifier);
|
||||
public join(socket : ExSocketInterface, userPosition: PointInterface): void {
|
||||
const user = new User(socket.userId, userPosition, false, this.positionNotifier, socket);
|
||||
this.users.set(socket.userId, user);
|
||||
// Let's call update position to trigger the join / leave room
|
||||
//this.updatePosition(socket, userPosition);
|
||||
|
@ -2,11 +2,27 @@ import Axios from "axios";
|
||||
import {API_URL} from "./Enum/EnvironmentVariable";
|
||||
import {MessageUI} from "./Logger/MessageUI";
|
||||
import {
|
||||
BatchMessage, ClientToServerMessage, GroupDeleteMessage, GroupUpdateMessage, ItemEventMessage, JoinRoomMessage,
|
||||
PositionMessage, RoomJoinedMessage, ServerToClientMessage,
|
||||
SetPlayerDetailsMessage, SetUserIdMessage, SilentMessage, UserJoinedMessage, UserLeftMessage, UserMovedMessage,
|
||||
BatchMessage,
|
||||
ClientToServerMessage,
|
||||
GroupDeleteMessage,
|
||||
GroupUpdateMessage,
|
||||
ItemEventMessage,
|
||||
JoinRoomMessage,
|
||||
PositionMessage,
|
||||
RoomJoinedMessage,
|
||||
ServerToClientMessage,
|
||||
SetPlayerDetailsMessage,
|
||||
SetUserIdMessage,
|
||||
SilentMessage,
|
||||
UserJoinedMessage,
|
||||
UserLeftMessage,
|
||||
UserMovedMessage,
|
||||
UserMovesMessage,
|
||||
ViewportMessage
|
||||
ViewportMessage,
|
||||
WebRtcDisconnectMessage,
|
||||
WebRtcSignalToClientMessage,
|
||||
WebRtcSignalToServerMessage,
|
||||
WebRtcStartMessage
|
||||
} from "./Messages/generated/messages_pb"
|
||||
|
||||
import {PlayerAnimationNames} from "./Phaser/Player/Animation";
|
||||
@ -132,7 +148,7 @@ export interface RoomJoinedMessageInterface {
|
||||
export class Connection implements Connection {
|
||||
private readonly socket: WebSocket;
|
||||
private userId: number|null = null;
|
||||
private batchCallbacks: Map<string, Function[]> = new Map<string, Function[]>();
|
||||
private listeners: Map<string, Function[]> = new Map<string, Function[]>();
|
||||
private static websocketFactory: null|((url: string)=>any) = null;
|
||||
|
||||
public static setWebsocketFactory(websocketFactory: (url: string)=>any): void {
|
||||
@ -185,13 +201,7 @@ export class Connection implements Connection {
|
||||
throw new Error('Unexpected batch message type');
|
||||
}
|
||||
|
||||
const listeners = this.batchCallbacks.get(event);
|
||||
if (listeners === undefined) {
|
||||
continue;
|
||||
}
|
||||
for (const listener of listeners) {
|
||||
listener(payload);
|
||||
}
|
||||
this.dispatch(event, payload);
|
||||
}
|
||||
} else if (message.hasRoomjoinedmessage()) {
|
||||
const roomJoinedMessage = message.getRoomjoinedmessage() as RoomJoinedMessage;
|
||||
@ -212,11 +222,32 @@ export class Connection implements Connection {
|
||||
this.userId = (message.getSetuseridmessage() as SetUserIdMessage).getUserid();
|
||||
} else if (message.hasErrormessage()) {
|
||||
console.error(EventMessage.MESSAGE_ERROR, message.getErrormessage()?.getMessage);
|
||||
} 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');
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private dispatch(event: string, payload: unknown): void {
|
||||
const listeners = this.listeners.get(event);
|
||||
if (listeners === undefined) {
|
||||
return;
|
||||
}
|
||||
for (const listener of listeners) {
|
||||
listener(payload);
|
||||
}
|
||||
}
|
||||
|
||||
public static createConnection(name: string, characterLayersSelected: string[]): Promise<Connection> {
|
||||
return Axios.post(`${API_URL}/login`, {name: name})
|
||||
.then((res) => {
|
||||
@ -363,7 +394,7 @@ export class Connection implements Connection {
|
||||
}
|
||||
|
||||
public onUserJoins(callback: (message: MessageUserJoined) => void): void {
|
||||
this.onBatchMessage(EventMessage.JOIN_ROOM, (message: UserJoinedMessage) => {
|
||||
this.onMessage(EventMessage.JOIN_ROOM, (message: UserJoinedMessage) => {
|
||||
callback(this.toMessageUserJoined(message));
|
||||
});
|
||||
}
|
||||
@ -383,30 +414,30 @@ export class Connection implements Connection {
|
||||
}
|
||||
|
||||
public onUserMoved(callback: (message: UserMovedMessage) => void): void {
|
||||
this.onBatchMessage(EventMessage.USER_MOVED, callback);
|
||||
this.onMessage(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);
|
||||
private onMessage(eventName: string, callback: Function): void {
|
||||
let callbacks = this.listeners.get(eventName);
|
||||
if (callbacks === undefined) {
|
||||
callbacks = new Array<Function>();
|
||||
this.batchCallbacks.set(eventName, callbacks);
|
||||
this.listeners.set(eventName, callbacks);
|
||||
}
|
||||
callbacks.push(callback);
|
||||
}
|
||||
|
||||
public onUserLeft(callback: (userId: number) => void): void {
|
||||
this.onBatchMessage(EventMessage.USER_LEFT, (message: UserLeftMessage) => {
|
||||
this.onMessage(EventMessage.USER_LEFT, (message: UserLeftMessage) => {
|
||||
callback(message.getUserid());
|
||||
});
|
||||
}
|
||||
|
||||
public onGroupUpdatedOrCreated(callback: (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => void): void {
|
||||
this.onBatchMessage(EventMessage.GROUP_CREATE_UPDATE, (message: GroupUpdateMessage) => {
|
||||
this.onMessage(EventMessage.GROUP_CREATE_UPDATE, (message: GroupUpdateMessage) => {
|
||||
callback(this.toGroupCreatedUpdatedMessage(message));
|
||||
});
|
||||
}
|
||||
@ -424,7 +455,7 @@ export class Connection implements Connection {
|
||||
}
|
||||
|
||||
public onGroupDeleted(callback: (groupId: number) => void): void {
|
||||
this.onBatchMessage(EventMessage.GROUP_DELETE, (message: GroupDeleteMessage) => {
|
||||
this.onMessage(EventMessage.GROUP_DELETE, (message: GroupDeleteMessage) => {
|
||||
callback(message.getGroupid());
|
||||
});
|
||||
}
|
||||
@ -438,37 +469,58 @@ export class Connection implements Connection {
|
||||
}
|
||||
|
||||
public sendWebrtcSignal(signal: unknown, receiverId: number) {
|
||||
/* return this.socket.emit(EventMessage.WEBRTC_SIGNAL, {
|
||||
receiverId: receiverId,
|
||||
signal: signal
|
||||
} as WebRtcSignalSentMessageInterface);*/
|
||||
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);
|
||||
}
|
||||
|
||||
public sendWebrtcScreenSharingSignal(signal: unknown, receiverId: number) {
|
||||
/* return this.socket.emit(EventMessage.WEBRTC_SCREEN_SHARING_SIGNAL, {
|
||||
receiverId: receiverId,
|
||||
signal: signal
|
||||
} as WebRtcSignalSentMessageInterface);*/
|
||||
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);
|
||||
}
|
||||
|
||||
public receiveWebrtcStart(callback: (message: WebRtcStartMessageInterface) => void) {
|
||||
// TODO
|
||||
// this.socket.on(EventMessage.WEBRTC_START, callback);
|
||||
public receiveWebrtcStart(callback: (message: UserSimplePeerInterface) => void) {
|
||||
this.onMessage(EventMessage.WEBRTC_START, (message: WebRtcStartMessage) => {
|
||||
callback({
|
||||
userId: message.getUserid(),
|
||||
name: message.getName(),
|
||||
initiator: message.getInitiator()
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public receiveWebrtcSignal(callback: (message: WebRtcSignalReceivedMessageInterface) => void) {
|
||||
// TODO
|
||||
// return this.socket.on(EventMessage.WEBRTC_SIGNAL, callback);
|
||||
this.onMessage(EventMessage.WEBRTC_SIGNAL, (message: WebRtcSignalToClientMessage) => {
|
||||
callback({
|
||||
userId: message.getUserid(),
|
||||
signal: JSON.parse(message.getSignal())
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public receiveWebrtcScreenSharingSignal(callback: (message: WebRtcSignalReceivedMessageInterface) => void) {
|
||||
// TODO
|
||||
// return this.socket.on(EventMessage.WEBRTC_SCREEN_SHARING_SIGNAL, callback);
|
||||
this.onMessage(EventMessage.WEBRTC_SCREEN_SHARING_SIGNAL, (message: WebRtcSignalToClientMessage) => {
|
||||
callback({
|
||||
userId: message.getUserid(),
|
||||
signal: JSON.parse(message.getSignal())
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public onServerDisconnected(callback: (event: CloseEvent) => void): void {
|
||||
this.socket.addEventListener('close', (event) => {
|
||||
|
||||
console.log('Socket closed with code '+event.code+". Reason: "+event.reason);
|
||||
if (event.code === 1000) {
|
||||
// Normal closure case
|
||||
return;
|
||||
@ -483,8 +535,11 @@ export class Connection implements Connection {
|
||||
}
|
||||
|
||||
disconnectMessage(callback: (message: WebRtcDisconnectMessageInterface) => void): void {
|
||||
// TODO
|
||||
// this.socket.on(EventMessage.WEBRTC_DISCONNECT, callback);
|
||||
this.onMessage(EventMessage.WEBRTC_DISCONNECT, (message: WebRtcDisconnectMessage) => {
|
||||
callback({
|
||||
userId: message.getUserid()
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
emitActionableEvent(itemId: number, event: string, state: unknown, parameters: unknown): void {
|
||||
@ -501,7 +556,7 @@ export class Connection implements Connection {
|
||||
}
|
||||
|
||||
onActionableEvent(callback: (message: ItemEventMessageInterface) => void): void {
|
||||
this.onBatchMessage(EventMessage.ITEM_EVENT, (message: ItemEventMessage) => {
|
||||
this.onMessage(EventMessage.ITEM_EVENT, (message: ItemEventMessage) => {
|
||||
callback({
|
||||
itemId: message.getItemid(),
|
||||
event: message.getEvent(),
|
||||
|
@ -82,7 +82,7 @@ export class SimplePeer {
|
||||
mediaManager.getCamera().then(() => {
|
||||
|
||||
//receive message start
|
||||
this.Connection.receiveWebrtcStart((message: WebRtcStartMessageInterface) => {
|
||||
this.Connection.receiveWebrtcStart((message: UserSimplePeerInterface) => {
|
||||
this.receiveWebrtcStart(message);
|
||||
});
|
||||
|
||||
@ -95,17 +95,22 @@ export class SimplePeer {
|
||||
});
|
||||
}
|
||||
|
||||
private receiveWebrtcStart(data: WebRtcStartMessageInterface) {
|
||||
this.WebRtcRoomId = data.roomId;
|
||||
this.Users = data.clients;
|
||||
private receiveWebrtcStart(user: UserSimplePeerInterface) {
|
||||
//this.WebRtcRoomId = data.roomId;
|
||||
this.Users.push(user);
|
||||
// Note: the clients array contain the list of all clients (even the ones we are already connected to in case a user joints a group)
|
||||
// So we can receive a request we already had before. (which will abort at the first line of createPeerConnection)
|
||||
// TODO: refactor this to only send a message to connect to one user (rather than several users).
|
||||
// TODO: refactor this to only send a message to connect to one user (rather than several users). => DONE
|
||||
// This would be symmetrical to the way we handle disconnection.
|
||||
//console.log('Start message', data);
|
||||
|
||||
//start connection
|
||||
this.startWebRtc();
|
||||
//this.startWebRtc();
|
||||
console.log('receiveWebrtcStart. Initiator: ', user.initiator)
|
||||
if(!user.initiator){
|
||||
return;
|
||||
}
|
||||
this.createPeerConnection(user);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -129,6 +134,7 @@ export class SimplePeer {
|
||||
if(
|
||||
this.PeerConnectionArray.has(user.userId)
|
||||
){
|
||||
console.log('Peer connection already exists to user '+user.userId)
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -49,6 +49,11 @@ message UserMovesMessage {
|
||||
ViewportMessage viewport = 2;
|
||||
}
|
||||
|
||||
message WebRtcSignalToServerMessage {
|
||||
int32 receiverId = 1;
|
||||
string signal = 2;
|
||||
}
|
||||
|
||||
message ClientToServerMessage {
|
||||
oneof message {
|
||||
JoinRoomMessage joinRoomMessage = 1;
|
||||
@ -57,6 +62,8 @@ message ClientToServerMessage {
|
||||
ViewportMessage viewportMessage = 4;
|
||||
ItemEventMessage itemEventMessage = 5;
|
||||
SetPlayerDetailsMessage setPlayerDetailsMessage = 6;
|
||||
WebRtcSignalToServerMessage webRtcSignalToServerMessage = 7;
|
||||
WebRtcSignalToServerMessage webRtcScreenSharingSignalToServerMessage = 8;
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,14 +139,20 @@ message RoomJoinedMessage {
|
||||
repeated ItemStateMessage item = 3;
|
||||
}
|
||||
|
||||
message WebRtcStartMessage {
|
||||
int32 userId = 1;
|
||||
string name = 2;
|
||||
bool initiator = 3;
|
||||
}
|
||||
|
||||
/*message WebRtcStartMessage {
|
||||
int32 itemId = 1;
|
||||
string event = 2;
|
||||
string stateJson = 3;
|
||||
string parametersJson = 4;
|
||||
}*/
|
||||
message WebRtcDisconnectMessage {
|
||||
int32 userId = 1;
|
||||
}
|
||||
|
||||
message WebRtcSignalToClientMessage {
|
||||
int32 userId = 1;
|
||||
string signal = 2;
|
||||
}
|
||||
|
||||
message ServerToClientMessage {
|
||||
oneof message {
|
||||
@ -147,9 +160,9 @@ message ServerToClientMessage {
|
||||
ErrorMessage errorMessage = 2;
|
||||
RoomJoinedMessage roomJoinedMessage = 3;
|
||||
SetUserIdMessage setUserIdMessage = 4; // TODO: merge this with RoomJoinedMessage ?
|
||||
// WebRtcStartMessage webRtcStartMessage = 3;
|
||||
// WebRtcSignalMessage webRtcSignalMessage = 4;
|
||||
// WebRtcScreenSharingSignalMessage webRtcScreenSharingSignalMessage = 5;
|
||||
// WebRtcDisconnectMessage webRtcDisconnectMessage = 6;
|
||||
WebRtcStartMessage webRtcStartMessage = 5;
|
||||
WebRtcSignalToClientMessage webRtcSignalToClientMessage = 6;
|
||||
WebRtcSignalToClientMessage webRtcScreenSharingSignalToClientMessage = 7;
|
||||
WebRtcDisconnectMessage webRtcDisconnectMessage = 8;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user