Vinitha Kumar
Vinitha Kumar

Reputation: 11

writing websocket c# data to a json file in nodejs

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...

Upvotes: 1

Views: 302

Answers (3)

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.');
    });
    
});

Upvotes: 0

eglease
eglease

Reputation: 2754

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.

Upvotes: 0

Etamiin
Etamiin

Reputation: 11

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.

Upvotes: 1

Related Questions