D
D
Dmitry Afonchenko2018-12-28 15:04:18
C++ / C#
Dmitry Afonchenko, 2018-12-28 15:04:18

Why does React get an error when trying to connect a websocket from C# code?

Good afternoon, comrades! I'm still new to React, and I can't figure out why this code doesn't work as it should.

componentDidMount = () => {
        this.ws = new WebSocket('ws://localhost:3000/ws');
        this.ws.addEventListener('message', function(e) {console.log('Server: ' + e.data);})
    }

From this code I am trying to send a message to a web socket. At the moment ws.Connect(); freeze occurs
using (var ws = new WebSocket("ws://localhost:3000/ws"))
      {
        ws.OnMessage += (sender, e) =>
          Console.WriteLine("Laputa says: " + e.Data);

        ws.Connect();
        ws.Send("BALUS");
        Console.ReadKey(true);
      }

In the browser logs at this moment:
websocket.js:80 WebSocket connection to 'ws://localhost:3000/sockjs-node/256/ixu2w3gz/websocket' failed: WebSocket is closed before the connection is established.

// at some point I disable my C# application and the development server crashes
WebSocket connection to 'ws://localhost:3000/ws' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
POST http://localhost:3000/sockjs-node/256/e4o5pyqd/xhr_streaming?t=1545998206736 net::ERR_CONNECTION_RESET 200 (OK)
The development server has disconnected.
Refresh the page if necessary.

What am I missing? Tried using redux, react-websocket - same nonsense.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question