Comments (10)
Sure thing.
If you'd like to send me more code, I can try to point you in the right direction. I'm at a loss at the moment. If you'd prefer, we can take this "offline". My email address is here: https://github.com/bminer
from ws-wrapper.
Thanks for posting. I didn't realize it was the server that was crashing with a TypeError. Sorry for glossing over that detail.
When you set the name
property of a WebSocketWrapper
, your code will break. This is because the WebSocketWrapper
extends a WebSocketChannel
, and you aren't allowed to set the name
property on a WebSocketChannel
. In fact, I would recommend against setting any property on the WebSocketChannel
.
The best solution to this problem is to use the .set()
method to set data for a particular WebSocketWrapper. For example:
const WebSocketServer = require('ws').Server
const WebSocketWrapper = require('ws-wrapper')
const wss = new WebSocketServer({port: 3000})
wss.on('connection', (rawSocket) => {
const socket = new WebSocketWrapper(rawSocket)
// Use `set` method to store custom data on the Socket (this is always safe)
socket.set('name', `Socket${Math.random()}`)
socket.on('echo', (data) => {
console.log('Echo', data, socket.name)
socket.emit('echo', data)
})
});
from ws-wrapper.
A few side notes (not related to this issue):
- I'd recommend taking a look at ws-server-wrapper if you're planning on using Node.js as the server. An example app is provided.
- Tests: I don't have any. It's quite typical of me unfortunately. This is a want but not a need at the moment. To be honest, I will graciously accept tests from nearly any testing framework. I've always been a fan of mocha + Node.js core assert.
from ws-wrapper.
Thanks for submitting a bug report.
It's crazy because I see nothing wrong with your code, and when I tested it, it worked properly.
One thing I noticed was your error message: TypeError: Converting circular structure to JSON
. ws-wrapper does not support serializing circular JSON structures, so you have avoid passing function arguments to socket.emit(...)
if one of the arguments refers to back upon itself somehow.
The following code will fail, for example:
const socket = new WebSocketWrapper(new WebSocket('ws://localhost:3000'))
var obj = {};
obj.hello = "world";
obj.myself = obj; // Don't do this!
socket.emit('echo-me', obj)
from ws-wrapper.
If you're still having trouble, you are welcome to post back here with more code, and I'd be happy to help. It's cool to see others using this lib. Best of luck!
Closing this issue for now. Please feel free to re-open if needed.
EDIT: The source of this issue is documented below:
wss.on("connection", (rawSocket) => {
const socket = new WebSocketWrapper(rawSocket)
// The following line it what is causing this Error
socket.name = `Socket${Math.random()}`;
socket.on('echo', (data) => {
console.log('Echo', data, socket.name)
})
});
from ws-wrapper.
@bminer Thanks for looking into this! In fact I didn't pass anything to socket.emit(...)
but '123'. Seems something is wrong with my setup.
from ws-wrapper.
@bminer I hid some information which I thought was irrelevant to the issue.
Here is the repro I made: OEvgeny/ws-wrapper-example
Please take a look at this line which causes original issue. It was a bit surprising to me 😄
Not sure is it something expected or not.
from ws-wrapper.
Also I noticed there are no any tests yet. If you have any preferences just let me know. But I'm not sure I'll have any time to contribute in near future.
from ws-wrapper.
Thanks for the information. All of these tips are very useful!
As for original issue I have an idea. Looks like it is possible to make produced by WebSocketWrapper constructor object frozen. Which will make error message more cleaner.
from ws-wrapper.
Really good idea. I will open an issue for that!
from ws-wrapper.
Related Issues (14)
- Channel does not have `get()` or `set()` methods
- onMessage hook? HOT 6
- Type definitions HOT 3
- Hooks HOT 9
- Option to disable pendingSend globally HOT 14
- Export ES Modules HOT 2
- Change constructor property names to avoid weird issues
- node app as client HOT 1
- Compile to ES5 HOT 1
- Maybe emit "connect" event in addition to "open" event? HOT 1
- whos better this or socket io? HOT 9
- "a" leading sendEvent data HOT 3
- Protocol mismatch: packets are sent as { "a": { "0": "connect" }} instead of { "a": ["connect"] } HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ws-wrapper.