category: Plugins

This article contains details about the simple chat plugin for Stormancer servers.

Initializing the plugin

To create a chat on you server application, you can create a specific scene using the "Chat" Template.

you can also add all the route to your scene using a special extension method in tuhe builder of your scene

ChatServerExtension.AddChat(ISceneHost yourScene)

Let the chat know who you are

The server simply broadcast every received message to every connected peers.

Before trying to use the chat, you should send your infos so the other peers can show who you are.

To do so, Create a ChatUserInfo, fill its User field and send it on the "UpdateInfo" route. The clientId will be set by the server and used by it so it remembers these infos comes from you.

public struct ChatUserInfo
    public long ClientId;
    public string User;

You can write whatever you want on the User field: a simple nickname or a json containing several datas.

You can change your client information after registeration by sending a new ChatUserInfo on the "UpdateInfo" route.

The server will not check if the UserInfo is unique. To do so, you'll have to implement it by yourself.

If no UserInfo exists when the chat broadcast a message, it will send a ChatUserInfo containing the clientId and an empty string so you can create your own annonymous system.

Sending and receiving messages

To Send messages, just send a string on the "chat" route.

The server will put your message on a ChatMessageDTO, with your userinfo and a timestamp in milliseconds (server clock).

public struct ChatMessageDTO
    public ChatUserInfo UserInfo;
    public string Message;
    public long TimeStamp;

To receive message, add a route named "chat". The ChatMessageDTO will be put on the received packet.

scene.AddRoute("chat", OnMessageReceived);

void OnMessageReceived(Packet<IScenePeer> packet
     var dto = packet.ReadObject<ChatMessageDTO>();

This dto will contain everything you need to correctly show the message and who sent it in your chat window.

Chat Events

You can request a list of every connected clients by sending an RPC on the "GetUsersInfos" route. You will receive a list containing every ChatUserInfo currently active on your Chat.

scene.AddProcedure("GetUserInfo", OnGetUserInfo);

Task OnGetUserInfo(RequestContext<IScenePeer> ctx)
    List<ChatUserInfo> users = ctx.ReadObject<List<ChatUserInfo>>();

When a player updates his infos, the new ChatUserInfo will be broadcasted to every connected peer using the "UpdateInfo" route.

When a player disconnects, a ChatMessageDTO will be sent on the "DiscardInfo" route. The DTO will contains the concerned user infos in a ChatUserInfo and the reasons on the message string.