- Install required packages for simStubsGen: see simStubsGen's README
- Checkout, compile and install into CoppeliaSim:
$ git clone https://github.com/CoppeliaRobotics/simWS.git
$ cd simWS
$ git checkout coppeliasim-v4.5.0-rev0
$ mkdir build && cd build
$ cmake -DCMAKE_BUILD_TYPE=Release ..
$ cmake --build .
$ cmake --install .
NOTE: replace coppeliasim-v4.5.0-rev0
with the actual CoppeliaSim version you have.
Use simWS.start
to start listening on the specified port, simWS.setMessageHandler
to set an handler for incoming messages, and simWS.send
to send messages on the specified connection:
-- Simple echo server
function onMessage(server,connection,data)
simWS.send(server,connection,data)
end
function sysCall_init()
server=simWS.start(9000)
simWS.setMessageHandler(server,'onMessage')
end
It is possible to broadcast a message to all connected clients by tracking active connections via open/close events:
-- Simple broadcaster
function onOpen(server,connection)
clients[server]=clients[server] or {}
clients[server][connection]=1
end
function onClose(server,connection)
clients[server][connection]=nil
end
function broadcast(server,data)
for connection,_ in pairs(clients[server] or {}) do
simWS.send(server,connection,data)
end
end
function sysCall_init()
clients={}
server=simWS.start(9000)
simWS.setOpenHandler(server,'onOpen')
simWS.setCloseHandler(server,'onClose')
end
See also the examples in the examples
subdirectory.