NetCoreServer icon indicating copy to clipboard operation
NetCoreServer copied to clipboard

One Client

Open arsium opened this issue 5 years ago • 10 comments

Hello dear how to send one message to a specific client ?

arsium avatar Dec 21 '19 16:12 arsium

Hi @arsium ,

please find the below example code. like that you can send a message to specific client.

TcpSession session = Server.FindSession(new Guid("<id of that particular client>"));
Console.WriteLine($"session value {session}");
session?.SendAsync($"Message text here");

note: here server denotes object of the TcpServer by NetCoreServer

Mohamed-Abubucker avatar Mar 07 '20 16:03 Mohamed-Abubucker

Thx !

arsium avatar Mar 08 '20 15:03 arsium

Hello @chronoxor , but how do you know the original client Guid?. Perhaps is necessary in OnConnected() return the Guid that was assign to the client and in the main app store in a dictionary and unique identifier for the client (it maybe the ipdaddress:port) and in the value the Guid for that client

wario8a avatar Oct 22 '20 01:10 wario8a

Each session and client has unique Id property with Guid type. You can access to all connected sessions in server using Sessions property or handle them in your own server implementation overriding OnConnected(TcpSession session) handler.

chronoxor avatar Oct 29 '20 23:10 chronoxor

Thought I would add.... if you're adding statements to the OnWsReceived function (or surrounding statements) you can simply type SendTextAsync("yourMessageHere") and it will send a reply to the client whose session is established in this context.

Example:

        public override void OnWsReceived(byte[] buffer, long offset, long size)
        {
            string message = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
            Console.WriteLine("Incoming: " + message);

            SendTextAsync($"You sent {message}");

            // If the buffer starts with '!' the disconnect the current session
            if (message == "!")
                Close(1000);
        }

Would reply with the message sent.

Correct me if I'm wrong guys? 😊

asheroto avatar Jan 05 '22 15:01 asheroto

Hello,

@Mohamed-Abubucker I used your code to try to send a message to a session, but for some reason the client is not receiving the message.

image image

I tried Send and SendAsync

Any ideas?

image

asheroto avatar Mar 08 '22 04:03 asheroto

For WebSockets protocol you need to call SendText()/SendTextAsync()/SendBinary()/SendBinaryAsync() methods

chronoxor avatar Mar 08 '22 04:03 chronoxor

For some reason I only have these methods...

image

asheroto avatar Mar 08 '22 05:03 asheroto

Try (ChatSession)session

chronoxor avatar Mar 08 '22 05:03 chronoxor

Perfect! Thank you very much.

Final working code to send a message to a specific session:

// Send to specific session
ChatSession session = (ChatSession)server.FindSession(new Guid(sessionID));
Console.WriteLine($"sending to session ID {sessionID} message {message}");
session?.SendTextAsync(message);

asheroto avatar Mar 08 '22 06:03 asheroto