Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onMessage fired twice when receiving Json data #45

Closed
sbland opened this issue Jul 17, 2019 · 0 comments
Closed

onMessage fired twice when receiving Json data #45

sbland opened this issue Jul 17, 2019 · 0 comments

Comments

@sbland
Copy link

sbland commented Jul 17, 2019

When receiving a message that is a stringified json object the onMessage function is called twice. First with the string version and then with the string parsed to a JSON object.

Server setup:

var server = require('websocket').server;
var socket = new server({
    httpServer: http.createServer().listen(8008),
});
socket.on('request', function(request) {
    const connection = request.accept(null, request.origin);
    connection.send("{\"test\":\"message\"}");
})

Client setup


const WebsocketManagerFunc = () => {
  const messageReceived = (data) => {
    console.log('received data');
    console.log(data);
    console.log(typeof data);
  };

  return (
    <Websocket
      url={`ws://localhost:8008/`}
      onMessage={messageReceived}
    />
  );
};

console log

Websocket connected
received data
{"test":"message"}
string
received data
{test: "message"}
object
@sbland sbland closed this as completed Jul 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant