Giter Site home page Giter Site logo

Comments (10)

bminer avatar bminer commented on May 31, 2024 1

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.

bminer avatar bminer commented on May 31, 2024 1

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.

bminer avatar bminer commented on May 31, 2024 1

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.

bminer avatar bminer commented on May 31, 2024

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.

bminer avatar bminer commented on May 31, 2024

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.

OEvgeny avatar OEvgeny commented on May 31, 2024

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

OEvgeny avatar OEvgeny commented on May 31, 2024

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

OEvgeny avatar OEvgeny commented on May 31, 2024

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.

OEvgeny avatar OEvgeny commented on May 31, 2024

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.

bminer avatar bminer commented on May 31, 2024

Really good idea. I will open an issue for that!

from ws-wrapper.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.