Adding USER_LEFT message to protobuf

This commit is contained in:
David Négrier 2020-09-24 16:11:47 +02:00
parent 2dad601311
commit 0c4c43f88c
3 changed files with 25 additions and 5 deletions

View File

@ -27,7 +27,7 @@ import {
SetPlayerDetailsMessage,
SubMessage,
UserMovedMessage,
BatchMessage, GroupUpdateMessage, PointMessage, GroupDeleteMessage, UserJoinedMessage
BatchMessage, GroupUpdateMessage, PointMessage, GroupDeleteMessage, UserJoinedMessage, UserLeftMessage
} from "../Messages/generated/messages_pb";
import {UserMovesMessage} from "../Messages/generated/messages_pb";
import Direction = PositionMessage.Direction;
@ -580,8 +580,7 @@ export class IoSocketController {
const clientListener = this.searchClientByIdOrFail(listener.id);
if (thing instanceof User) {
const clientUser = this.searchClientByIdOrFail(thing.id);
clientListener.emit(SocketIoEvent.USER_LEFT, clientUser.userId);
//console.log("Sending USER_LEFT event");
this.emitUserLeftEvent(clientListener, clientUser.userId);
} else if (thing instanceof Group) {
this.emitDeleteGroupEvent(clientListener, thing.getId());
} else {
@ -629,6 +628,17 @@ export class IoSocketController {
emitInBatch(client, SocketIoEvent.GROUP_DELETE, subMessage);
}
private emitUserLeftEvent(socket: Socket, userId: number): void {
const userLeftMessage = new UserLeftMessage();
userLeftMessage.setUserid(userId);
const subMessage = new SubMessage();
subMessage.setUserleftmessage(userLeftMessage);
const client : ExSocketInterface = socket as ExSocketInterface;
emitInBatch(client, SocketIoEvent.USER_LEFT, subMessage);
}
/**
*
* @param socket

View File

@ -4,7 +4,7 @@ import {MessageUI} from "./Logger/MessageUI";
import {
BatchMessage, GroupDeleteMessage, GroupUpdateMessage,
PositionMessage,
SetPlayerDetailsMessage, UserJoinedMessage, UserMovedMessage,
SetPlayerDetailsMessage, UserJoinedMessage, UserLeftMessage, UserMovedMessage,
UserMovesMessage,
ViewportMessage
} from "./Messages/generated/messages_pb"
@ -170,6 +170,9 @@ export class Connection implements Connection {
} else if (message.hasUserjoinedmessage()) {
event = EventMessage.JOIN_ROOM;
payload = message.getUserjoinedmessage();
} else if (message.hasUserleftmessage()) {
event = EventMessage.USER_LEFT;
payload = message.getUserleftmessage();
} else {
throw new Error('Unexpected batch message type');
}
@ -320,7 +323,9 @@ export class Connection implements Connection {
}
public onUserLeft(callback: (userId: number) => void): void {
this.socket.on(EventMessage.USER_LEFT, callback);
this.onBatchMessage(EventMessage.USER_LEFT, (message: UserLeftMessage) => {
callback(message.getUserid());
});
}
public onGroupUpdatedOrCreated(callback: (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => void): void {

View File

@ -53,6 +53,7 @@ message SubMessage {
GroupUpdateMessage groupUpdateMessage = 2;
GroupDeleteMessage groupDeleteMessage = 3;
UserJoinedMessage userJoinedMessage = 4;
UserLeftMessage userLeftMessage = 5;
}
}
@ -76,3 +77,7 @@ message UserJoinedMessage {
repeated string characterLayers = 3;
PositionMessage position = 4;
}
message UserLeftMessage {
int32 userId = 1;
}