This is the "low-level" connection API for using websockets.
openWebSocket(config)
: opens a websocket connection to the url specified in an<input id="url" type="text">
on port 9998.config
: is used to configure functions to be called on certain events.
var config = { "onOpen": function() { console.log("Websocket open!"); }, "onMessage": { "worldStatus": function(msg) { // is an array of rooms spawned in the Unity world // msg[i].objectId // msg[i].xPos // msg[i].zPos // msg[i].rot }, "vrPosition": function(msg) { // has the xPos and zPos of the VR player at 30Hz // msg.xPos // msg.zPos // msg.rot // msg.maxHealth // msg.currentHealth }, "mobPositions": function(msg) { // is an array of mobs spawned in the unity world // msg[i].objectId // msg[i].xPos // msg[i].zPos // msg[i].id // msg[i].dead // msg[i].rot // msg[i].maxHealth // msg[i].currentHealth } }, "onClose": function() { console.log("Websocket closed!"); } }
closeWebSocket()
: cleanly terminates the websocket connection.
This builds on top of webSockets.js
and provides helpful methods for sending different types of commands.
UnityClient.buildCommand(objectId, xPos, zPos, rot)
: sends a correctly formatted buildCommand to Unity for an object of prefab typeobjectId
at position (xPos
,zPos
) with rotationrot
.UnityClient.spawnMobCommand(objectId, xPos, zPos, id)
: sends a correctly formatted spawnMobCommand to Unity to spawn a mob associated toid
using prefab typeobjectId
at (xPos
,zPos
).