在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:sebastianseilund/node-json-socket开源软件地址:https://github.com/sebastianseilund/node-json-socket开源编程语言:JavaScript 100.0%开源软件介绍:JsonSocket
InstallationYou can install
Or add it to your {
...
"dependencies": {
"json-socket": "*"
}
...
} And then run:
Usage
Simple client/server exampleHere is a simple example where a client can connect to the server and send two numbers (a and b) that the server multiplies and sends back the result. Servervar net = require('net'),
JsonSocket = require('json-socket');
var port = 9838;
var server = net.createServer();
server.listen(port);
server.on('connection', function(socket) { //This is a standard net.Socket
socket = new JsonSocket(socket); //Now we've decorated the net.Socket to be a JsonSocket
socket.on('message', function(message) {
var result = message.a + message.b;
socket.sendEndMessage({result: result});
});
}); Clientvar net = require('net'),
JsonSocket = require('json-socket');
var port = 9838; //The same port that the server is listening on
var host = '127.0.0.1';
var socket = new JsonSocket(new net.Socket()); //Decorate a standard net.Socket with JsonSocket
socket.connect(port, host);
socket.on('connect', function() { //Don't send until we're connected
socket.sendMessage({a: 5, b: 7});
socket.on('message', function(message) {
console.log('The result is: '+message.result);
});
}); Streaming exampleHere is an example of a server that can stream square numbers. A client can connect and send a Servervar net = require('net'),
JsonSocket = require('json-socket');
var port = 9838;
var server = net.createServer();
server.listen(port);
server.on('connection', function(socket) {
socket = new JsonSocket(socket);
var n;
var isRunning = false;
var streatTimeout;
socket.on('message', function(message) {
if (message.command == 'start') {
if (!isRunning) {
n = message.beginAt || 1;
isRunning = true;
streamInterval = setInterval(function() {
socket.sendMessage(n * n);
n++;
}, 1000);
}
} else if (message.command == 'stop') {
if (isRunning) {
isRunning = false;
clearInterval(streamInterval);
}
}
});
}); Clientvar net = require('net'),
JsonSocket = require('json-socket');
var port = 9838; //The same port that the server is listening on
var host = '127.0.0.1';
var socket = new JsonSocket(new net.Socket()); //Decorate a standard net.Socket with JsonSocket
socket.connect(port, host);
socket.on('connect', function() { //Don't send until we're connected
socket.sendMessage({command: 'start', beginAt: 10});
socket.on('message', function(square) {
console.log(square);
if (square > 200) {
socket.sendMessage({command: 'stop'});
}
});
}); Will output:
Sending a single messageJsonSocket has two helper methods that are useful if you just need to send a single message and you don't need the socket to stay open. The second method is var JsonSocket = require('json-socket');
JsonSocket.sendSingleMessage(port, host, {type: 'ping'}, function(err) {
if (err) {
//Something went wrong
throw err;
}
console.log('Pinged '+host+' on port '+port);
}); The second method is var JsonSocket = require('json-socket');
JsonSocket.sendSingleMessageAndReceive(port, host, {type: 'ping'}, function(err, message) {
if (err) {
//Something went wrong
throw err;
}
console.log('Server said: '+message.type); //E.g. pong
}); Message formatsThe messages can be any Javascript object that can be converted to JSON:
Extra methods exposed on JsonSocket instancesSince Besides that the following methods and events are also available on JsonSocket.sendSingleMessage(port, host, message, callback)Sends a single message anc close the connection instantly. Arguments
JsonSocket.sendSingleMessageAndReceive(port, host, message, callback)Sends a single message to Arguments
socket.sendMessage(message, callback)Sends a JSON a message over the socket. Arguments
socket.sendEndMessage(message, callback)Same as No more messages can be sent from either the server or client through this socket. socket.sendError(err, callback)Convenience method for sending an error as a message. Arguments
Example socket.sendError(new Error('Something went wrong!'); Will send a message of this JSON format: {
"success": false,
"error": "Something went wrong"
} socket.sendEndError(err, callback)Same as No more messages can be sent from either the server or client through this socket. socket.isClosed()Returns true if either the server or the client has closed the connection. Returns false otherwise. Event: 'message'
Emitted when a complete message has been received. How the protocol worksThe Example: socket.sendMessage({
type: "ping"
}); Will send a message that looks like this:
This mechanism ensures that messages that are chunked will be parsed correctly. ##Todo
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论