diff --git a/NativeWebSocket/Assets/WebSocketExample/Connection.cs b/NativeWebSocket/Assets/WebSocketExample/Connection.cs index ff14385..5f35fe3 100644 --- a/NativeWebSocket/Assets/WebSocketExample/Connection.cs +++ b/NativeWebSocket/Assets/WebSocketExample/Connection.cs @@ -13,7 +13,8 @@ public class Connection : MonoBehaviour async void Start() { // websocket = new WebSocket("ws://echo.websocket.org"); - websocket = new WebSocket("ws://localhost:8080"); + // websocket = new WebSocket("ws://localhost:8080"); + websocket = new WebSocket("ws://my-glitch-url.glitch.me", new Dictionary() { { "User-Agent", "Unity3D" } }); websocket.OnOpen += () => { diff --git a/NodeServer/index.js b/NodeServer/index.js index 7172373..b503342 100644 --- a/NodeServer/index.js +++ b/NodeServer/index.js @@ -1,12 +1,8 @@ const crypto = require('crypto'); -const express = require('express'); const { createServer } = require('http'); const WebSocket = require('ws'); -const app = express(); - -const server = createServer(app); -const wss = new WebSocket.Server({ server }); +const wss = new WebSocket.Server({ port: 3000 }); wss.on('connection', function(ws) { console.log("client joined."); @@ -32,8 +28,4 @@ wss.on('connection', function(ws) { clearInterval(textInterval); clearInterval(binaryInterval); }); -}); - -server.listen(8080, function() { - console.log('Listening on http://localhost:8080'); -}); +}); \ No newline at end of file diff --git a/README.md b/README.md index 8f9f69f..4298490 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,16 @@ +This is a modified branch on my fork of [endel/NativeWebSocket](https://github.com/endel/NativeWebSocket) to work with Glitch.com + +After making the Glitch.com project from this repo, open the project's terminal and run: +``` +npm install +``` +and then +``` +refresh +``` +**This only needs to be done when you create the project or add packages. Do not run `npm start` as Glitch will already be running the server automatically. Instead of the terminal, the server's `console.log` output will show up in the Glitch project's logs.** +*These modifications successfully worked with Unity 2021.2.8f1. It will probably work on other versions but I didn't test it.* + Native WebSocket This is the simplest and easiest WebSocket library for Unity you can find! diff --git a/package.json b/package.json new file mode 100644 index 0000000..40c1f3c --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "hello-unity-nativewebsocket-glitch", + "version": "0.0.1", + "description": "A simple Node server for Unity, running on Glitch.com.", + "main": "index.js", + "scripts": { + "start": "node NodeServer/index.js" + }, + "author": { + "name": "Endel Dreyer", + "url": "https://github.com/endel" + }, + "contributors": [ + {"name":"Ben Ukhanov","url":"https://github.com/benukhanov"}, + {"name":"Gaeel Bradshaw-Rodriguez","url":"https://github.com/Bradshaw"}, + {"name":"Tom Bogin","url":"https://github.com/mentaman"}, + {"name":"Evan Binder","url":"https://github.com/evnb"}, + {"name":"Ryan Yang","url":"https://github.com/ryqndev"} + ], + "engines": { + "node": "12.x" + }, + "repository": { + "url": "https://github.com/evnb/NativeWebSocket/tree/glitch" + }, + "license": "ISC", + "dependencies": { + "ws": "^8.4.2" + }, + "keywords": [ + "node", + "glitch", + "unity" + ] +} \ No newline at end of file