ClusterWS-Client-JS icon indicating copy to clipboard operation
ClusterWS-Client-JS copied to clipboard

Cant run in Node

Open fundon opened this issue 7 years ago • 18 comments

Submitting

  • [ ] Bug
  • [x] Question
  • [ ] Suggetion
  • [ ] Other

Details

ReferenceError: window is not defined
    at Object.<anonymous> (node_modules/clusterws-client-js/index.js:86:14)
   at Module._compile (module.js:652:30)

fundon avatar May 14 '18 17:05 fundon

Unfortunatly this client was designing only for browsers, there are plans to build node version but it would be as separate project.

goriunov avatar May 15 '18 00:05 goriunov

Thanks, ASAP. :)

fundon avatar May 15 '18 04:05 fundon

https://github.com/fundon/ClusterWS-Client-JS/commit/66deec3d9200ddd9410a7e907c23f46ed5d2c5e0#diff-f41e9d04a45c83f3b6f6e630f10117fe

fundon avatar May 15 '18 13:05 fundon

That looks pretty good :)

goriunov avatar May 16 '18 01:05 goriunov

So is this now a part of the release? because I still get this error

var Socket = window.MozWebSocket || window.WebSocket, ClusterWS = function() {
             ^

ReferenceError: window is not defined

slidenerd avatar Sep 03 '18 11:09 slidenerd

@slidenerd not yet, i am planning to include that in next release for now you can use fork. Make sure u use uws version 10.148.1 without ^ at the front.

goriunov avatar Sep 03 '18 19:09 goriunov

Will be in version 4.0 (probable separate repository or different deploy build to do not ship useless deps and code with browser version)

goriunov avatar Oct 18 '18 10:10 goriunov

btw, putting this before you require clusterws-client-js lets this work in node:

global.window = {WebSocket: require('ws')};

cryptiklemur avatar Dec 08 '18 19:12 cryptiklemur

@aequasi that is a good solutions :)ws API should be the same as browsers ws (i think that ws supports it so it should work ok)

goriunov avatar Dec 09 '18 23:12 goriunov

You should also be able to do global.window = {WebSocket: require('@clusterws/cws').WebSocket};, but i havent tested it

cryptiklemur avatar Dec 10 '18 23:12 cryptiklemur

@aequasi that one will not work out as cws does not have browser interface (i will probably add it) cws uses event based interface so instead of writing

ws.onopen = function() {
 // some logic
}

you need to write:

ws.on("open", () => {
 // some logic
})

i will probably write a simple wrapper for cws to handle browser interface, thanks for the tip :)

goriunov avatar Dec 11 '18 23:12 goriunov

I have release version 0.9.0 of @clusterws/cws with browser interface support and from now @aequasi proposal should work correctly:

 global.window = {WebSocket: require('@clusterws/cws').WebSocket}

goriunov avatar Dec 14 '18 05:12 goriunov

@goriunov I'm not sure if this is related to this, but I'm using @clusterws/cws version 0.10.0 and I get this error when I establish a connection to the server or receive any message:

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at websocket.onmessage (/home/vlad/NodeJS/clusterws-test/node_modules/clusterws-client-js/index.js:145:32)
    at WebSocket.emit (/home/vlad/NodeJS/clusterws-test/node_modules/@clusterws/cws/dist/index.js:19:14)
    at native.client.group.onMessage (/home/vlad/NodeJS/clusterws-test/node_modules/@clusterws/cws/dist/index.js:40:7)

The server receives messages just fine, but if I send something back to the client I get that error, the message is always a JSON object.

I haven't tried the library in the browser, but I do want to use this to write some server-side tests. If I use the standard ws module, I don't get any errors, but obviously sending data does not work (only establishing the connection)

vladikus10 avatar Jan 11 '19 06:01 vladikus10

@vladikus10 can you try updating clusterws-client-js to version 3.2.0, and if it works let me know :)

goriunov avatar Jan 11 '19 07:01 goriunov

@goriunov Updated and the error is gone, thanks!

vladikus10 avatar Jan 11 '19 07:01 vladikus10

@goriunov In Node.js, Blob and FileReader are missing. New v4 alpha version.

https://github.com/ClusterWS/ClusterWS-Client-JS/blob/next/src/index.ts#L221-L226

fundon avatar May 20 '19 06:05 fundon

@fundon thanks, i have not yet tested with Node, will have look soon :)

goriunov avatar May 20 '19 06:05 goriunov

@fundon just released new version alpha 2 with that fix, thank you :)

goriunov avatar May 20 '19 08:05 goriunov