Is it possible to send a socket.io message to my localhost server (node) using the command line in linux?
My socket.io code looks like this:
var io = require('socket.io');
var socket;
socket = io.listen(server, {log: false});
server.listen(8081);
socket.sockets.on('connection', function (socket) {
socket.on('message', function (data)
{
console.log("Received socket message from GUI: "+data);
}
});
I'd like to send a socket.io message that looks like this: "testControl,loadModels,start"
which then triggers some server side logic. Is this possible?
I came across websocketd which seems promising, but it creates a websocket in linux, I just want to send a message to an existing websocket.
Version Info:
send() The WebSocket. send() method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of bufferedAmount by the number of bytes needed to contain the data.
Sending Messages With WebSocketYou can send both text and binary data through a WebSocket. For your demo application you need to send the contents of the textarea to the server when the form is submitted. To do this you first need to set up an event listener on the form.
For a more low-level (socket.io-independent) solution you can use my tool websocat.
Example session for communicating with socket.io's chat example:
$ websocat 'wss://socket-io-chat.now.sh/socket.io/?transport=websocket'
< 0{"sid":"yGVElkNCXVgc5w_JOVtc","upgrades":[],"pingInterval":25000,"pingTimeout":60000}
< 40
> 2
< 3
> 42["add user", "websocat"]
< 42["login",{"numUsers":15}]
> 42["new message", "Hello from websocat"]
< 42["typing",{"username":"1223"}]
< 42["new message",{"username":"1223","message":"Reply from browser."}]
< 42["stop typing",{"username":"1223"}]
> 42["new message", "OK, may flood again."]
>
denotes messages to be typed.
You can use iocat cli client from npm which is based on Socket.IO.
$ iocat --socketio ws://127.0.0.1:8081
< Hello there
I was able to work around this by doing the following:
Create a new endpoint in your server
app.post('/sendSocketMessage', function(req, res){
console.log("Request is "+JSON.stringify(req.body));
socketManager.parse(req.body); //send message directly to your socket parser
res.status(200).send("OK");
});
Send data using curl
curl --noproxy localhost,
-X POST
--data "data=testControl,loadModels,start,"
http://localhost:8081/sendSocketMessage
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With