Unity client 1 → endel / NativeWebSocets - > WebSocketSendMessage(data) → Send to server
Server PHP Ratchet - > transfers data by session
Unity client 2 → endel / NativeWebSocets - > WebSocketReceiveMessage(string data) → Receive from server
That is, my server works as a message transmitter, movement data is transmitted to the server and from there it is transmitted to other users who are in this room (sessions)
It works, but here’s how to do it now with entities 1.0.0 (NetCode)
How there are options, how to communicate between clients?
Netcode for entities is a server authoritative networking solution. Clients send commands/rpc to the server and can only receive data from server.
There is not client-client direct communication.
The server is in charge of simulate the world and send to the client authoritative snapshot (ghosts). Can also send RPCs to all clients as well and act as a relay (receive and RPC, send back to all other clients).
So yes the flow is perfectly possible. though in that case I don’t see the need for using Netcode for Entities. Probably, if you just need to relay messages, it is more straightforward and flexible to use the UTP (Unity Transport Package) directly (that also netcode uses) for that.
but in entities there is such a possibility?
that the example with a cube worked through a dedicated server?
I have a working version through endel/NativeWebSocets, but it’s not on entities, I just don’t know yet how to cross it, is it possible what will come of it…
I’m sorta confused. The cube sample work with a dedicated server, sure!.
You need to build a server (use the Dedicated Server Platform for that), deploy it somewhere (can be on a local network too, you just need to know the address in that case) and the connect a client to it using either the Editor or a player build (Client only).
You can also use websocket for the server too if you need, but you need to implement a custom driver builder for it (that is a more advanced topic, but not hard to do).
If you instead mean having another server/service, that is not a Unity player or executable, and that just relay messages, that will not work. The server is in charge of the simulation state and responsible to serialise the data to the client.
But I’m not getting what you are trying to do.
I originally wrote what construction I have now (without entities)
Unity client 1 → endel / NativeWebSocets - > WebSocketSendMessage(data) → Send to server Server PHP Ratchet - > transfers data by session Unity client 2 → endel / NativeWebSocets - > WebSocketReceiveMessage(string data) → Receive from server
but now, since my project works on entities, the data transfer also needs to be done using them.
I see that in entities there are similar methods for transmitting / receiving data
that’s why I think not to touch the server part, and leave it as a relay, and in the client set the direction to this dedicated server. is it possible?
or on the server you need a certain software?
Is there a description of the full setup cycle with a dedicated server? maybe I don’t understand something…
it looks like we are talking about different things, I meant the server is not on unity, it is a regular Linux-based server, and PHP Ratchet acts as a repeater…
the script on the server determines the session and sends it to everyone who is in this session. Question - how to configure entities to send over sockets? is it possible? If so, how?
In order to use Netcode for Entities, you cannot use your current server. You need use a Unity build (player) that contains the server and that handle the connections and the simulation. If you just need to use the Entities package, and you don’t need to run the simulation on the server but only send data (raw bytes, string whatsoever) you don’t need the Netcode for Entities at all.
Yes it is possible and because you need to just do that (sending raw string/bytes data) I suggested:
sorry for the inconvenience, but I’m confused, I actually need to really transfer the string (different data) to the server, and the server, in turn, sends this string to the client, and that’s it. if you say that entities are not needed for this, then it remains to figure out how to combine them …
Then I see such a model - a MonoBehaviour object that will transmit data and receive, my web class will work there, then it remains to understand how to send data from entities there and then send it back to the entity from this object?
It is not an inconvenience. What I was trying to say is that you are confusing the purpose of the Netcode For Entities package. And that probably you don’t need either Transport too, because you still need/want to use your Websocket class/service abstraction.
To send entities data I think it is easier to if you create a System that retrieve the Monobehavior you want to interact with or that contains / access that Web class singleton.
Then grab the data from the Entities you need, create a string or serialize that data somehow (up to you), and then just invoke the send method.
Attention though that awaiting inside the Update is not what you want probably (well, for sending you don’t want to await in general).
For receiving, depends how you handle that. I presume async and not from another thread. Regardless, the data access should be thread safe and on the main thread.
If the data is received from/inside a Monobehavior and you want to set the data back to entities, in general you need to:
get the world in which you want to transfer the data into.
get the entities you need to apply the data to. Query? Single entity? I don’t know, depend what you need.
convert back from string to entity data (deserialise). And since this is managed data, and we don’t provide any convenience for that, is up to you.
If you are using a System for that, again you need to grab your Websocket class, check if there is new data, get the received data and consume it in a similar fashion:
get the entities you need to apply the data to.
convert back from string to entity data (deserialise). And since this is managed data, and we don’t provide any convenience for that, is up to you.