ClusterWS / ClusterWS-Client-JS

:fire: JavaScript Client for ClusterWS - lightweight, fast and powerful framework for building scalable WebSocket applications in Node.js.
MIT License
115 stars 10 forks source link

Cant run in Node #44

Open fundon opened 6 years ago

fundon commented 6 years ago

Submitting

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)
goriunov commented 6 years ago

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

fundon commented 6 years ago

Thanks, ASAP. :)

fundon commented 6 years ago

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

goriunov commented 6 years ago

That looks pretty good :)

slidenerd commented 6 years ago

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
goriunov commented 6 years ago

@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 commented 5 years ago

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

cryptiklemur commented 5 years ago

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

global.window = {WebSocket: require('ws')};
goriunov commented 5 years ago

@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)

cryptiklemur commented 5 years ago

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

goriunov commented 5 years ago

@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 commented 5 years ago

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}
vladikus10 commented 5 years ago

@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)

goriunov commented 5 years ago

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

vladikus10 commented 5 years ago

@goriunov Updated and the error is gone, thanks!

fundon commented 5 years ago

@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

goriunov commented 5 years ago

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

goriunov commented 5 years ago

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