Adding JOIN_ROOM message in protobuf

This commit is contained in:
David Négrier 2020-09-24 14:50:28 +02:00
parent 72806b3ca0
commit 2dad601311
3 changed files with 37 additions and 5 deletions

View File

@ -27,7 +27,7 @@ import {
SetPlayerDetailsMessage,
SubMessage,
UserMovedMessage,
BatchMessage, GroupUpdateMessage, PointMessage, GroupDeleteMessage
BatchMessage, GroupUpdateMessage, PointMessage, GroupDeleteMessage, UserJoinedMessage
} from "../Messages/generated/messages_pb";
import {UserMovesMessage} from "../Messages/generated/messages_pb";
import Direction = PositionMessage.Direction;
@ -541,9 +541,17 @@ export class IoSocketController {
const clientListener = this.searchClientByIdOrFail(listener.id);
if (thing instanceof User) {
const clientUser = this.searchClientByIdOrFail(thing.id);
const messageUserJoined = new MessageUserJoined(clientUser.userId, clientUser.name, clientUser.characterLayers, clientUser.position);
clientListener.emit(SocketIoEvent.JOIN_ROOM, messageUserJoined);
const userJoinedMessage = new UserJoinedMessage();
userJoinedMessage.setUserid(clientUser.userId);
userJoinedMessage.setName(clientUser.name);
userJoinedMessage.setCharacterlayersList(clientUser.characterLayers);
userJoinedMessage.setPosition(ProtobufUtils.toPositionMessage(clientUser.position));
const subMessage = new SubMessage();
subMessage.setUserjoinedmessage(userJoinedMessage);
emitInBatch(clientListener, SocketIoEvent.JOIN_ROOM, subMessage);
} else if (thing instanceof Group) {
this.emitCreateUpdateGroupEvent(clientListener, thing);
} else {

View File

@ -4,7 +4,7 @@ import {MessageUI} from "./Logger/MessageUI";
import {
BatchMessage, GroupDeleteMessage, GroupUpdateMessage,
PositionMessage,
SetPlayerDetailsMessage, UserMovedMessage,
SetPlayerDetailsMessage, UserJoinedMessage, UserMovedMessage,
UserMovesMessage,
ViewportMessage
} from "./Messages/generated/messages_pb"
@ -15,6 +15,7 @@ import {PlayerAnimationNames} from "./Phaser/Player/Animation";
import {UserSimplePeerInterface} from "./WebRtc/SimplePeer";
import {SignalData} from "simple-peer";
import Direction = PositionMessage.Direction;
import {ProtobufClientUtils} from "./Network/ProtobufClientUtils";
enum EventMessage{
WEBRTC_SIGNAL = "webrtc-signal",
@ -166,6 +167,9 @@ export class Connection implements Connection {
} else if (message.hasGroupdeletemessage()) {
event = EventMessage.GROUP_DELETE;
payload = message.getGroupdeletemessage();
} else if (message.hasUserjoinedmessage()) {
event = EventMessage.JOIN_ROOM;
payload = message.getUserjoinedmessage();
} else {
throw new Error('Unexpected batch message type');
}
@ -283,7 +287,19 @@ export class Connection implements Connection {
}
public onUserJoins(callback: (message: MessageUserJoined) => void): void {
this.socket.on(EventMessage.JOIN_ROOM, callback);
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);
});
}
public onUserMoved(callback: (message: UserMovedMessage) => void): void {

View File

@ -52,6 +52,7 @@ message SubMessage {
UserMovedMessage userMovedMessage = 1;
GroupUpdateMessage groupUpdateMessage = 2;
GroupDeleteMessage groupDeleteMessage = 3;
UserJoinedMessage userJoinedMessage = 4;
}
}
@ -68,3 +69,10 @@ message GroupUpdateMessage {
message GroupDeleteMessage {
int32 groupId = 1;
}
message UserJoinedMessage {
int32 userId = 1;
string name = 2;
repeated string characterLayers = 3;
PositionMessage position = 4;
}