1

I have following code as a server:

var http = require('http'),
    cors = require('cors'),
    connect = require('connect'),
    WebSocketServer = require('ws');

var DataController = function() {
    this.PORT = 8090;
    this.startWS();
    this.startServer();
}

DataController.prototype = {
    // code to start server...

    getMessage: function(_message) {
        var that = this,
        message = JSON.parse(_message),
        msgName = message.name;
        console.log('new message ' + msgName);
    switch(msgName){
        case 'start':
        // here is some code to perform actions
}

As you can see here I have getMessage function that fires some events and to invoke it I need to send a message start. That's easy enough, right? Surpisingly for me, it's not. That's how I try to perform this .send() method in my React component (my React app is running on different port of course):

import React, { Component } from 'react';

class App extends Component {

    componentDidMount() {
        const socket = new WebSocket('ws://localhost:8090');
        socket.onopen = function() {
            socket.send(JSON.stringify({message: "start"}));
        }
    }
    render() {
        return (
            <div className="container">App</div>
        )
    }
 }

 export default App;

What I try to accomplish here - I need to send just one word - start to actually start receieving data from server but I can't and do not understand why. See, there's a console.log() statement in my server code that logs a message start when it's sent. So it actually should log start but it logs undefined instead. What I tried so far: 1. Send just start 2. Define an object like this:

const data = {
    message: "start"
}

And then pass data to .send() function 3. Define just a variable like this: const data = "start"

And nothing above works!Everything returns undefined on server side. I'm just getting started with websockets and can't find a way to get it working. I refered mostly to Mozilla and it didn't work too. So what actually am I doing wrong in this case? Thanks in advance, I appreciate any help!

3
  • You might need to bind the socket.onopen handler to the correct context? Commented Jul 17, 2017 at 22:25
  • A lot of people I know learned with socket.io to get started. If this is your first exposure to sockets, in general, I definitely recommend it as a good launchpad. Commented Jul 17, 2017 at 22:29
  • Try to use different ports for html-server and websocket, e.g. 8090 and 8091. Commented Jul 17, 2017 at 22:30

1 Answer 1

2

It appears to me that the object you are sending over in your .send() does not contain a name key, so when you then define msgName as message.name it evaluates to undefined as that key does not exist in the object.

You can change this easily by changing the object you send over so that the key "start" is tied to would be called name.

Sign up to request clarification or add additional context in comments.

2 Comments

Kee, you saved me on this one, it was really easy as just changing message to name. Damn, I should really take some rest by night instead of cracking issues :) Many thanks!
I'm glad I could help! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.