3

I am working on a small project in C# where I need to create a socket client and watch the socket for a json file.

I have tried a few thing and every time I run the project I get the error:

PM|Fatal|StackBuilderSink._PrivateProcessMessage|WebSocketSharp.WebSocketExceptio n: An exception has occurred while reading an HTTP request/response. ---> System.IO.EndOfStreamException: The header cannot be read from the data source.

Some background. For testing I am using node.js with socket.io to read a Json file and io.emit the data. I know the data is formatted correctly I have a HTML/java script page that sees the data and does stuff with it.

public MainWindow()
{
    InitializeComponent();

    using (var ws = new WebSocket("ws://127.0.0.1:8889"))
    {
        ws.OnMessage += (sender, e) =>
              json_output.Text = (e.Data);

        ws.ConnectAsync();
    }
}
lebelinoz
  • 4,890
  • 10
  • 33
  • 56
Jyeomans
  • 71
  • 4
  • I believe that the problem my be that my test server and the live server this will be working with does not provide a handshake. is there a way to bypass the handshake and just listen to the socket feed? – Jyeomans Oct 19 '17 at 18:59
  • I encountered the same error. In my case, the problem was https://stackoverflow.com/questions/9745249/websocket-with-ssl – Elgun Cumayev Apr 12 '22 at 13:26

1 Answers1

0

From your description I assume that you send the plain json-file through io.emit.

The hypertext transport protocol (HTTP) expects the respone built as described in wikipedia (HTTP response):

The response message consists of the following:

  • A status line which includes the status code and reason message (e.g., HTTP/1.1 200 OK, which indicates that the client's request
    succeeded).
  • Response header fields (e.g., Content-Type: text/html).
  • An empty line.
  • An optional message body.

Therefore you have to send the status line and response header information first, followed by an empty line. After this you can send the content of the json-file as the message body.

I assume there is also a library for node.js that will work as a web server and handle the HTTP stuff.


If you want to handle a plain data stream without a protocol, you should not use a protocol specific socket like WebSocket but a generic one like TcpListener.

Of course you have then to solve the problems that should be solved by a protocol like when is the end of a message/file ...

Examples of how to use TcpListener and pitholes to avoid should be available here on stackoverflow or via Dr. Google.

Community
  • 1
  • 1
Roland Bär
  • 1,720
  • 3
  • 22
  • 33
  • so the problem i have not is that the server that is providing the json file in the live test is coming from a server i cant change. i just built my test server to match. is there a way to just watch the server for the data and not require the formality? basically i am working with a digital signage system on a closed network. i am just trying to interrupt the data flow with my system and gave i user some control in the data feed. – Jyeomans Oct 21 '17 at 15:52