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_srcdirectory to your server. - Run
npm installin 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.jsfile. - Run
node app.jsto start the server. Note: Potentially, you need to start the server usingsudo node app.jsfor 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.unityscene in Unity3D. - Edit
WEBSOCKET_SERVER_URLin theWebSocketServerConnector.csscript. - Run the application, and follow the example keyboard interaction: (1) Press
Cto open connection to the server; (2) PressSto send a default example message. - Optional: For further handling of the default example message, refer to the implementation and documentation in the
WebSocketReceivedMessageHandler.csclass.
- Navigate to the websocket-sharp GitHub repository, and clone / download the repository.
- Open
websocket-sharp.slnin the repository using Visual Studio. - Remove
Example,Example1,Example2, andExample3from the solution. - Change build option from
DebugtoRelease. - Select
Build websocket-sharpfrom theBuildmenu in Visual Studio. - The build should be
successful. - Navigate to
websocket-sharp/bin/Releaseand copy thewebsocket-sharp.dllfile into theAssetsdirectory 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.htmlin thejs_srcdirectory to run the JavaScript client locally in your web browser. Alternatively: Copy the contents of thejs_srcdirectory 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.OnMessageevent in theWebSocketServerConnector.csclass, resulting in aWebSocket.OnErrorevent. For this purpose, theWebSocketReceivedMessageHandler.csclass has been implemented to provide a work-around accordingly. - Extend the communication interface by implementing further event handlers.
- Added
WebSocketReceivedMessageHandler.cshelper 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