Skip to content

Commit

Permalink
Make it work with Glitch.com
Browse files Browse the repository at this point in the history
This modified branch enables the repo to work with a server hosted on Glitch.com. Tested with Unity 2021.2.8f1

Co-Authored-By: Ryan Yang <rqyang@uci.edu>
  • Loading branch information
evnb and ryqndev committed Jan 23, 2022
1 parent fed568d commit bb9a933
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
3 changes: 2 additions & 1 deletion NativeWebSocket/Assets/WebSocketExample/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>() { { "User-Agent", "Unity3D" } });

websocket.OnOpen += () =>
{
Expand Down
12 changes: 2 additions & 10 deletions NodeServer/index.js
Original file line number Diff line number Diff line change
@@ -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.");
Expand All @@ -32,8 +28,4 @@ wss.on('connection', function(ws) {
clearInterval(textInterval);
clearInterval(binaryInterval);
});
});

server.listen(8080, function() {
console.log('Listening on http://localhost:8080');
});
});
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.*

<img src="Media/header.png?raw=true" alt="Native WebSocket" />

This is the simplest and easiest WebSocket library for Unity you can find!
Expand Down
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit bb9a933

Please sign in to comment.