Switched ITEM_EVENT to protobuf

This commit is contained in:
David Négrier 2020-09-24 17:24:37 +02:00
parent cd083a2090
commit 24a6cd7f8e
4 changed files with 81 additions and 15 deletions

View File

@ -27,7 +27,13 @@ import {
SetPlayerDetailsMessage, SetPlayerDetailsMessage,
SubMessage, SubMessage,
UserMovedMessage, UserMovedMessage,
BatchMessage, GroupUpdateMessage, PointMessage, GroupDeleteMessage, UserJoinedMessage, UserLeftMessage BatchMessage,
GroupUpdateMessage,
PointMessage,
GroupDeleteMessage,
UserJoinedMessage,
UserLeftMessage,
ItemEventMessage
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import {UserMovesMessage} from "../Messages/generated/messages_pb"; import {UserMovesMessage} from "../Messages/generated/messages_pb";
import Direction = PositionMessage.Direction; import Direction = PositionMessage.Direction;
@ -433,22 +439,42 @@ export class IoSocketController {
} }
}); });
socket.on(SocketIoEvent.ITEM_EVENT, (itemEvent: unknown) => { socket.on(SocketIoEvent.ITEM_EVENT, (message: unknown) => {
if (!isItemEventMessageInterface(itemEvent)) { if (!(message instanceof Buffer)) {
socket.emit(SocketIoEvent.MESSAGE_ERROR, {message: 'Invalid ITEM_EVENT message. Expecting binary buffer.'});
console.warn('Invalid ITEM_EVENT message received (expecting binary buffer): ', message);
return;
}
const itemEventMessage = ItemEventMessage.deserializeBinary(new Uint8Array(message));
const itemEvent = ProtobufUtils.toItemEvent(itemEventMessage);
/*if (!isItemEventMessageInterface(itemEvent)) {
socket.emit(SocketIoEvent.MESSAGE_ERROR, {message: 'Invalid ITEM_EVENT message.'}); socket.emit(SocketIoEvent.MESSAGE_ERROR, {message: 'Invalid ITEM_EVENT message.'});
console.warn('Invalid ITEM_EVENT message received: ', itemEvent); console.warn('Invalid ITEM_EVENT message received: ', itemEvent);
return; return;
} }*/
try { try {
const Client = (socket as ExSocketInterface); const Client = (socket as ExSocketInterface);
socket.to(Client.roomId).emit(SocketIoEvent.ITEM_EVENT, itemEvent); //socket.to(Client.roomId).emit(SocketIoEvent.ITEM_EVENT, itemEvent);
const world = this.Worlds.get(Client.roomId); const world = this.Worlds.get(Client.roomId);
if (!world) { if (!world) {
console.error("Could not find world with id '", Client.roomId, "'"); console.error("Could not find world with id '", Client.roomId, "'");
return; return;
} }
const subMessage = new SubMessage();
subMessage.setItemeventmessage(itemEventMessage);
// Let's send the event without using the SocketIO room.
for (let user of world.getUsers().values()) {
const client = this.searchClientByIdOrFail(user.id);
//client.emit(SocketIoEvent.ITEM_EVENT, itemEvent);
emitInBatch(client, SocketIoEvent.ITEM_EVENT, subMessage);
}
world.setItemState(itemEvent.itemId, itemEvent.state); world.setItemState(itemEvent.itemId, itemEvent.state);
} catch (e) { } catch (e) {
console.error('An error occurred on "item_event"'); console.error('An error occurred on "item_event"');

View File

@ -1,7 +1,8 @@
import {PointInterface} from "./PointInterface"; import {PointInterface} from "./PointInterface";
import {PositionMessage} from "../../Messages/generated/messages_pb"; import {ItemEventMessage, PositionMessage} from "../../Messages/generated/messages_pb";
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface"; import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
import Direction = PositionMessage.Direction; import Direction = PositionMessage.Direction;
import {ItemEventMessageInterface} from "_Model/Websocket/ItemEventMessage";
export class ProtobufUtils { export class ProtobufUtils {
@ -32,4 +33,23 @@ export class ProtobufUtils {
return position; return position;
} }
public static toItemEvent(itemEventMessage: ItemEventMessage): ItemEventMessageInterface {
return {
itemId: itemEventMessage.getItemid(),
event: itemEventMessage.getEvent(),
parameters: JSON.parse(itemEventMessage.getParametersjson()),
state: JSON.parse(itemEventMessage.getStatejson()),
}
}
public static toItemEventProtobuf(itemEvent: ItemEventMessageInterface): ItemEventMessage {
const itemEventMessage = new ItemEventMessage();
itemEventMessage.setItemid(itemEvent.itemId);
itemEventMessage.setEvent(itemEvent.event);
itemEventMessage.setParametersjson(JSON.stringify(itemEvent.parameters));
itemEventMessage.setStatejson(JSON.stringify(itemEvent.state));
return itemEventMessage;
}
} }

View File

@ -2,7 +2,7 @@ import Axios from "axios";
import {API_URL} from "./Enum/EnvironmentVariable"; import {API_URL} from "./Enum/EnvironmentVariable";
import {MessageUI} from "./Logger/MessageUI"; import {MessageUI} from "./Logger/MessageUI";
import { import {
BatchMessage, GroupDeleteMessage, GroupUpdateMessage, BatchMessage, GroupDeleteMessage, GroupUpdateMessage, ItemEventMessage,
PositionMessage, PositionMessage,
SetPlayerDetailsMessage, UserJoinedMessage, UserLeftMessage, UserMovedMessage, SetPlayerDetailsMessage, UserJoinedMessage, UserLeftMessage, UserMovedMessage,
UserMovesMessage, UserMovesMessage,
@ -173,6 +173,9 @@ export class Connection implements Connection {
} else if (message.hasUserleftmessage()) { } else if (message.hasUserleftmessage()) {
event = EventMessage.USER_LEFT; event = EventMessage.USER_LEFT;
payload = message.getUserleftmessage(); payload = message.getUserleftmessage();
} else if (message.hasItemeventmessage()) {
event = EventMessage.ITEM_EVENT;
payload = message.getItemeventmessage();
} else { } else {
throw new Error('Unexpected batch message type'); throw new Error('Unexpected batch message type');
} }
@ -400,16 +403,24 @@ export class Connection implements Connection {
this.socket.on(EventMessage.WEBRTC_DISCONNECT, callback); this.socket.on(EventMessage.WEBRTC_DISCONNECT, callback);
} }
emitActionableEvent(itemId: number, event: string, state: unknown, parameters: unknown) { emitActionableEvent(itemId: number, event: string, state: unknown, parameters: unknown): void {
return this.socket.emit(EventMessage.ITEM_EVENT, { const itemEventMessage = new ItemEventMessage();
itemId, itemEventMessage.setItemid(itemId);
event, itemEventMessage.setEvent(event);
state, itemEventMessage.setStatejson(JSON.stringify(state));
parameters itemEventMessage.setParametersjson(JSON.stringify(parameters));
});
this.socket.emit(EventMessage.ITEM_EVENT, itemEventMessage.serializeBinary().buffer);
} }
onActionableEvent(callback: (message: ItemEventMessageInterface) => void): void { onActionableEvent(callback: (message: ItemEventMessageInterface) => void): void {
this.socket.on(EventMessage.ITEM_EVENT, callback); this.onBatchMessage(EventMessage.ITEM_EVENT, (message: ItemEventMessage) => {
callback({
itemId: message.getItemid(),
event: message.getEvent(),
parameters: JSON.parse(message.getParametersjson()),
state: JSON.parse(message.getStatejson())
});
});
} }
} }

View File

@ -39,6 +39,14 @@ message UserMovesMessage {
ViewportMessage viewport = 2; ViewportMessage viewport = 2;
} }
/************ BI-DIRECTIONAL MESSAGES **************/
message ItemEventMessage {
int32 itemId = 1;
string event = 2;
string stateJson = 3;
string parametersJson = 4;
}
/*********** SERVER TO CLIENT MESSAGES *************/ /*********** SERVER TO CLIENT MESSAGES *************/
@ -54,6 +62,7 @@ message SubMessage {
GroupDeleteMessage groupDeleteMessage = 3; GroupDeleteMessage groupDeleteMessage = 3;
UserJoinedMessage userJoinedMessage = 4; UserJoinedMessage userJoinedMessage = 4;
UserLeftMessage userLeftMessage = 5; UserLeftMessage userLeftMessage = 5;
ItemEventMessage itemEventMessage = 6;
} }
} }