Skip to content

Example: 2 Chat

uwee edited this page Feb 10, 2019 · 4 revisions

The Chat example is not very complex. There are no private messages or channels. It builds on the SimpleConnection example by adding InsightModules for message handling.

Sending a chat message to the server:

public void HandleSendChat()
{
    insight.Send((short)MsgId.Chat, new ChatMsg() { 
        Origin = nameInput.text, 
        Data = chatInput.text });

    chatInput.text = ""; //Clear out the previously entered text from the field
}

Once the server receives the message it simply echoes it back to all the connected clients:

private void HandleChatMsg(InsightNetworkMessage netMsg)
{
    if(server.logNetworkMessages) { Debug.Log("[InsightServer] - HandleChatMsg()"); }

    ChatMsg message = netMsg.ReadMessage<ChatMsg>();

    server.SendToAll((short)MsgId.Chat, message); //Broadcast back to all other clients
}
Clone this wiki locally