1

I want get the realtime data from c# websocket and write that data to a json file from a nodejs server... how this can be achieved? The json file should be updated with the newer data...

  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Oct 15 '21 at 19:55

3 Answers3

1

See awnser on this link [Similar post] That can help getting the string from a WebSocket stream.

When you have your string, you can simply write the string in a json file with System.IO.File class.

Etamiin
  • 11
  • 1
  • Am sorry that didnt answer my question i want this to be done on nodejs server and not java! – Vinitha Kumar Oct 14 '21 at 19:40
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 15 '21 at 00:42
0

Looks like you have two different problems to solve.

The first is getting data from C# client to Node.js server over websockets. This can be done with Socket.io which has a .NET client.

The second part is writing this data into a JSON file on the Node.js side. This would depend on what data you are getting from the client. In the best case, you are getting a valid object and all you need to do is serialize it with JSON.stringify or something like it. Otherwise, you may need to write data to an object that you serialize or update the JSON file manually. The best solution would depend on your data and requirements.

eglease
  • 2,445
  • 11
  • 18
  • 28
0

I dont know what is your C# data, for example this next html code will simulate it, just open it in a browser. There is an input field and a button, button send the input content to nodejs websocket server, the next textarea shows the response of the server. In your C# code, if you send stringified json to ws://localhost:5000/ it will response to your C# app, if nodejs server runs. The base stringified json is {"a":2} in this example, you can modify, and add items in the field, and can see the result after pushed the button.

<!DOCTYPE html>
<html>
<head>
    <title>WebSocket JSON communication</title>
</head>
<body>
<input class="" name="" id="msgout" style="" value='{"a":2}' type="text" align="left" size="" onclick="" onchange="" onblur="" onfocus="" />
<input class="" name="" id="A" style="" value='' type="button" align="left" size="" onclick="toserver( document.getElementById('msgout').value );" onblur="" onfocus="" />
<br />
<textarea id="msgin" style="width:500px;height:600px;">
</textarea>
</body>
<script>


const ws = new WebSocket('ws://localhost:5000/');

var clientjson ={};
ws.onmessage = function(e) {
    document.getElementById("msgin").value+=e.data+"\n";
  clientjson=JSON.parse(e.data);
};

function toserver( content ) {
      ws.send( content );
      console.log("cs",content);
}
</script>
</html>

This is the nodejs websocket server, when get string, parse it to JSON object, merge with server JSON, then send the whole JSON to all clients back. The serverjson variable contains the whole merged data. This is a basic example, because you didnt specify your problem more detailed.

const http = require('http');
const WebSocket = require('ws');
const WebSocketServer = require('websocket').server;
const server = http.createServer();
server.listen(5000);
const wsServer = new WebSocketServer({
    httpServer: server
});

var connection;
var serverjson={};

wsServer.on('request', function(request) {
        try{
            connection = request.accept(null, request.origin);
            connection.on('message', function(message) {
            getjson=JSON.parse(message.utf8Data);
            serverjson={...serverjson, ...getjson};
            connection.sendUTF( JSON.stringify(serverjson,0,0,'') );
        }
        catch( e ){
            console.log( e );
        }
    });
    connection.on('close', function(reasonCode, description) {
        console.log('Client has disconnected.');
    });
    
});
  • Hi , thanks for your reply, I have a node server running on port 5000 and serving a html page from that server. I have C3# websocket url ws://localhost:5555/status/intersection.. using ws node js package am able to make websocket connection in node server, ws package does not work on browser, I think according to the documentation, they meant the client is not the browser, so my question is when i get data from the NodeJS server how to get the data from the front end(browser).I can make the connection directly from the html, but it has security issues I believe, hope You understand the issue. – Vinitha Kumar Dec 14 '21 at 15:36
  • 1. ws package drops me CORS error look solution above, the http server solve it. 2. connection.sendUTF() sends to all client, what connected to server, that message arrives to browser in html ws.onmessage function. I think, you would be better, if write your comment to the main problem. – Sándor Krisztián Dec 15 '21 at 19:21