Git Product home page Git Product logo

Comments (7)

igm avatar igm commented on August 23, 2024

Do you know what was the underlaying protocol for your communication - Websocke/Streaming/Polling? Also could you please make sure that the message from the client is in the format you prefer to be used on server? How do you create json messages on the client?

from sockjs-go.

druska avatar druska commented on August 23, 2024

I found out this is occurring because SockJS runs utils.quote on the input string, which basically JSON encodes the message again. The server should probably json.Unmarshal the message before passing it off in ReadMessage to remove this encoding.

See: https://github.com/sockjs/sockjs-client/blob/master/lib/sockjs.js#L248 and https://github.com/sockjs/sockjs-client/blob/master/lib/utils.js#L250

This is how I'm sending my message:

sock.send(JSON.stringify({
    'op': 'subscribe',
    'channel': 'mychannel'
}));

Which would turn into: ""{"op":"subscribe","channel":"mychannel"}""
Which would be json.Unmarshalled (in Go) to "{"op":"subscribe","channel":"mychannel"}" again

from sockjs-go.

igm avatar igm commented on August 23, 2024

well, you are probably right. sockjs uses a string protocol for messages and encodes each string by JSON.stringify() internally. The inverse operation should be performed on the server on each message.
I probably did a big mistake when defining ReadMessage() to return []byte. It should return "string". And then it would be clear. What you send from the client would be exactly what you get on the server.

I'll need to think about this a little more, this change would cause incompatibilities and I'm not sure how to safely introduce it at this version.

from sockjs-go.

druska avatar druska commented on August 23, 2024

I'm thinking maybe it could either be a config setting, or a separate Read function. This would maintain backwards compatibility.

from sockjs-go.

igm avatar igm commented on August 23, 2024

Feel free to send a patch/pull request if you have any.

from sockjs-go.

druska avatar druska commented on August 23, 2024

Sure, I'll probably create a patch when I can, probably some time this week. I found one other issue with the read format. If the sockjs-client sends 2 messages in rapid succession using xhr (haven't tested all protocols), the payload is sent as a single xhr request which is a JSON array of JSON objects:

["{\"op\":\"subscribe\",\"channel\":\"mychannel\"}","{\"op\":\"subscribe\",\"channel\":\"otherchannel\"}"]

This should probably be decoded into 2 ReadMessage() calls.
To not break old clients, I guess this should stay the same as well unless they use a new Read command or config setting. What do you think?

from sockjs-go.

igm avatar igm commented on August 23, 2024

fixed by #6

from sockjs-go.

Related Issues (20)

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.