This is an example of a Websocket server that uses a local database to keep all the clients updated with the information since the first player is connected.
This WebSocket server is working with Unity clients on multiplayer-websocket-client-unity.
npm installWhen player connects
{
"action": "connected",
"data": {
"username": "jsConnection"
}
}Sending player position and rotation in realtime
{
"action": "movement",
"data": {
"position": {
"x": 1,
"y": 2,
"z": 3
},
"rotation": {
"x": 4,
"y": 5,
"z": 6
}
}
}#Install nodemon -- npm install -g nodemon --
nodemon index.jsYou can also test with WSCat in multiple consoles
Connect to socket server
wscat -c ws://localhost:8080Send message
#When wscat is connected
#Connection
> { "action": "connected", "data": { "username": "jsConnection" } }
#Sending position and rotation
> {"action":"movement","data":{"position":{"x":1,"y":2,"z":3},"rotation":{"x":4,"y":5,"z":6}}}