Colyseus.js Websocket is not defined

Hi,
I'm working on a little server using your framework, following the examples (state-handler). All went well expect when I try to connect with a client from colyseus.js

import {Room, Client} from "colyseus.js";
const client = new Client("ws://" + config.me.host + ":" + config.me.port);

With same host and port for server obviously,
I have the following error :

D:\Documents\Devs\BattleArena\GameManager\node_modules\@gamestdio\websocket\lib\index.js:70
 */WebSocketClient.CONNECTING=WebSocket.CONNECTING;/**
                              ^
ReferenceError: WebSocket is not defined
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\node_modules\@gamestdio\websocket\lib\index.js:70:31)
    at Module._compile (module.js:635:30)
    at Module.replacementCompile (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:58:13)
    at module.exports (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\default-require-extensions\js.js:8:9)
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:62:4)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\node_modules\colyseus.js\lib\Connection.js:13:19)
    at Module._compile (module.js:635:30)
    at Module.replacementCompile (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:58:13)
    at module.exports (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\default-require-extensions\js.js:8:9)
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:62:4)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\node_modules\colyseus.js\lib\Client.js:5:20)
    at Module._compile (module.js:635:30)
    at Module.replacementCompile (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:58:13)
    at module.exports (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\default-require-extensions\js.js:8:9)
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:62:4)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\node_modules\colyseus.js\lib\index.js:4:16)
    at Module._compile (module.js:635:30)
    at Module.replacementCompile (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:58:13)
    at module.exports (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\default-require-extensions\js.js:8:9)
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:62:4)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\tests\room\battle.room.test.ts:4:1)
    at Module._compile (module.js:635:30)
    at Module.m._compile (D:\Documents\Devs\BattleArena\GameManager\node_modules\ts-node\src\index.ts:400:23)
    at Module.replacementCompile (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:58:13)
    at module.exports (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\default-require-extensions\js.js:8:9)
    at D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:62:4
    at Object.require.extensions.(anonymous function) [as .ts] (D:\Documents\Devs\BattleArena\GameManager\node_modules\ts-node\src\index.ts:403:12)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at D:\Documents\Devs\BattleArena\GameManager\node_modules\mocha\lib\mocha.js:250:27
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (D:\Documents\Devs\BattleArena\GameManager\node_modules\mocha\lib\mocha.js:247:14)
    at Mocha.run (D:\Documents\Devs\BattleArena\GameManager\node_modules\mocha\lib\mocha.js:576:10)
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\node_modules\mocha\bin\_mocha:637:18)
    at Module._compile (module.js:635:30)
    at Module.replacementCompile (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:58:13)
    at module.exports (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\default-require-extensions\js.js:8:9)
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\node_modules\append-transform\index.js:62:4)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at runMain (C:\Users\Valentin\.node-spawn-wrap-34680-266e6ebd9028\node:68:10)
    at Function.<anonymous> (C:\Users\Valentin\.node-spawn-wrap-34680-266e6ebd9028\node:171:5)
    at Object.<anonymous> (D:\Documents\Devs\BattleArena\GameManager\node_modules\nyc\bin\wrap.js:23:4)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at C:\Users\Valentin\.node-spawn-wrap-34680-266e6ebd9028\node:178:8
    at Object.<anonymous> (C:\Users\Valentin\.node-spawn-wrap-34680-266e6ebd9028\node:181:3)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
npm ERR! Test failed.  See above for more details.

I tried to use it in the example and I have the same problem. I tried installing ws, or uws but it changed nothing.

I didn't find anything either on internet about this problem

Does anyone have an idea to solve it ?

Thanks

I know this topic is 3 months old, but I have found a workaround so if anyone searches for this they will have an answer.

The reason your unit test is not working is that WebSocket is not globally installed when you run it in say Jest for example.

A quick work around would be to define it globally.

// npm install ws

const globalAny: any = global; // This way we don't get an error in TypeScript saying WebSocket is not a defined property on global
globalAny.WebSocket = require('ws');

Also, it appears as though the colyseus.js client makes use of the window.local storage object. A quick polyfill you could do would look something like this

globalAny.window = {
    localStorage: {
        getItem: () => {},
        setItem: () => {}
    },

};