The purpose of this project is to provide a simple, minimalistic workflow / template to illustrate communication and data transfer between a Unity3D client application and a JavaScript web client application via WebSocket connection implemented as a Node.js server.
- Unity3D and JavaScript clients: connect to a WebSocket server, send / receive text data (
string
) formatted as JSON, stringify / parse JSON objects, access JSON fields - Node.js server: setup WebSocket Secure server, retrieve and forward / pass-through of text data (
string
) messages formatted as JSON to all connected clients (excl. original sender) - Unity3D: helper class to conveniently handle and further process received messages via WebSocket connection, allowing for any desired (and Unity GameObject-related) action
- comprehensive documentation across all source files
This project has been built using the following specifications:
- Unity3D 2019.2.17f1 Personal
- websocket-sharp (accessed and compiled on 2020-10-26)
- JavaScript WebSocket WebAPI (accessed on 2020-10-26)
- Node.js v4.2.6
- npm 3.5.2
- ws 4.1.0; npm link
All source code can be found within the nodejs_src
directory of this repository. Carefully investigate the app.js
file to follow the implementation.
Quick start guide
- Copy the contents of the
nodejs_src
directory to your server. - Run
npm install
in order to locally install all required dependencies as listed inpackage.json
. - Edit
hostname
,port
, and (if needed) the paths to your key and certificate files (for TLS support) in theapp.js
file. - Run
node app.js
to start the server. Note: Potentially, you need to start the server usingsudo node app.js
for permission to access the key and certificate files on your server.
All source code can be found within the unity_src
directory of this repository. Carefully investigate the WebSocketServerConnector.cs
file to follow the implementation.
Quick start guide
- Copy the contents of the
unity_src/Assets/
directory into your Unity3D project. - Access, clone, build, and import websocket-sharp library to your Unity3D project (see websocket-sharp Unity3D import).
- Open the
DEMO_WebSocketServerConnector.unity
scene in Unity3D. - Edit
WEBSOCKET_SERVER_URL
in theWebSocketServerConnector.cs
script. - Run the application, and follow the example keyboard interaction: (1) Press
C
to open connection to the server; (2) PressS
to send a default example message. - Optional: For further handling of the default example message, refer to the implementation and documentation in the
WebSocketReceivedMessageHandler.cs
class.
- Navigate to the websocket-sharp GitHub repository, and clone / download the repository.
- Open
websocket-sharp.sln
in the repository using Visual Studio. - Remove
Example
,Example1
,Example2
, andExample3
from the solution. - Change build option from
Debug
toRelease
. - Select
Build websocket-sharp
from theBuild
menu in Visual Studio. - The build should be
successful
. - Navigate to
websocket-sharp/bin/Release
and copy thewebsocket-sharp.dll
file into theAssets
directory in your Unity3D project.
All source code can be found within the js_src
directory of this repository. Carefully investigate the index.html
file to follow the implementation.
Quick start guide
- Open the
index.html
in thejs_src
directory to run the JavaScript client locally in your web browser. Alternatively: Copy the contents of thejs_src
directory to your server, and navigate to it accordingly. - In your web browser, preferably open the browser's developer console to follow implemented
console.log()
messages. - Press the button
Send message.
to send a default example message.
Following, a list of known issues and some thoughts for further development:
- Unity GameObject-related manipulations are not possible directly from within the
WebSocket.OnMessage
event in theWebSocketServerConnector.cs
class, resulting in aWebSocket.OnError
event. For this purpose, theWebSocketReceivedMessageHandler.cs
class has been implemented to provide a work-around accordingly. - Extend the communication interface by implementing further event handlers.
- Added
WebSocketReceivedMessageHandler.cs
helper class in order to further handle received messages via WebSocketServerConnector's WebSocket.OnMessage event, allowing for any desired (and Unity GameObject-related) actions in the application.
MIT License, see LICENSE.md