Skip to content
This repository has been archived by the owner on Feb 25, 2020. It is now read-only.

Server Send does not work #48

Open
mahmah2 opened this issue May 21, 2019 · 2 comments
Open

Server Send does not work #48

mahmah2 opened this issue May 21, 2019 · 2 comments

Comments

@mahmah2
Copy link

mahmah2 commented May 21, 2019

Hi,

I have got stuck in a weird situation. I want to initiate a send from the server side to all of the clients. So logically this should work:

                private IServer _server
                 ...
                var connections = _server.GetConnections().GetConnections();
               
                foreach (ITcpConnection connection in connections)
                {
                    connection.Socket.Send(Encoding.ASCII.GetBytes("COMMAND1"));
                }

But client doesn't receive this command.

Now if I save the server context somewhere and reuse it later it works:

private ISender _sender;

ServerPacketHandler_OnProcess(object sender, string e)
{
            _sender = (sender as IPacketContext).Sender;
}

Now this one will work :

                _sender.Send<string>("COMMAND1");`

What is the correct way of initiating a send from the server. Its not in the example and tests either.

@erictuvesson
Copy link
Contributor

When you use Socket.Send you don't add the packet header and just send it as a raw string. So the client doesn't know what to do with it so it just ignores it.

I am not sure if this is on the master branch

When calling TcpSender.Send<T>(..) the packet is serialised with a header.
https://github.com/MarkioE/Networker/blob/feature/3.0/Networker/Common/TcpSender.cs#L21

@sbaum23
Copy link

sbaum23 commented Oct 28, 2019

@mahmah2 I was running into the same issue with the client not receiving my packets when using Socket.SendAsync. I was able to get it working using PacketSerialiserProvider.PacketSerialiser.Serialise<T>

Assuming the packet type I'm trying to send was:

public class MyAwesomePacket
{
   public int Id { get; set; }
}

Then using the Socket worked:

var socket = .... // code to get the Socket of the client you are wanting to send to.
var packet = new MyAwesomePacket { Id = 1 };
socket.SendAsync(PacketSerialiserProvider.PacketSerialiser.Serialise<MyAwesomePacket>(packet), SocketFlags.None);

For serialization I was using MessagePack.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants