bash 如何从 Linux 中的命令行向我的 socket.io websocket 发送消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42101839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:43:50  来源:igfitidea点击:

How do I send a message to my socket.io websocket from the command line in linux?

linuxbashwebsocketsocket.io

提问by Katie

Is it possible to send a socket.iomessageto my localhost server(node) using the command line in linux?

是否有可能发送socket.io消息给我的本地主机服务器使用在Linux命令行(节点)?

My socket.io code looks like this:

我的 socket.io 代码如下所示:

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?

我想发送一个看起来像这样的 socket.io 消息:"testControl,loadModels,start"然后触发一些服务器端逻辑。这可能吗?

I came across websocketdwhich seems promising, but it createsa websocket in linux, I just want to senda message to an existingwebsocket.

我碰到websocketd这很有前途,但它产生在Linux上的WebSocket,我只是想发送消息给现有的WebSocket。

Version Info:

版本信息:

  • Node Version: v0.10.42
  • Socket.IO Version: 1.3.4
  • Express Version: 4.12.0
  • 节点版本:v0.10.42
  • Socket.IO 版本:1.3.4
  • 快速版本:4.12.0

回答by mk12ok

You can write a simple client like this (let's name it clientwith no extension):

您可以像这样编写一个简单的客户端(让我们将其命名client为不带扩展名):

#!/usr/bin/env node
const socket = require('socket.io-client')('http://localhost:3000');
const someDelay = 10;
socket.on('connect', function () {
    console.log('connected...');
    if (process.argv[2] && process.argv[3]) {
        console.log('sending ' + process.argv[2] + ': ' + process.argv[3]);
        socket.emit(process.argv[2], process.argv[3]);
        setTimeout(() => {
            process.exit(0);  
        }, someDelay);
    } else {
        console.log('usage: ./client.js <event> <data>');
        process.exit(1);
    }
});

with a very basic package.json

用一个非常基本的 package.json

{
  "name": "client",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "client"
  },
  "dependencies": {
    "socket.io-client": "^1.4.6"
  }
}

Then run npm install, give clientpermissions to be executed and you can run it with (for example) ./client message testControl

然后运行npm install,授予client执行权限,您可以使用(例如)运行它./client message testControl

What do you think? ;)

你怎么认为?;)

回答by Katie

I was able to work around this by doing the following:

我能够通过执行以下操作来解决这个问题:

  1. 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");
    });
    
  2. Send data using curl

    curl --noproxy localhost, 
         -X POST 
         --data "data=testControl,loadModels,start," 
         http://localhost:8081/sendSocketMessage
    
  1. 在您的服务器中创建一个新端点

    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");
    });
    
  2. 使用 curl 发送数据

    curl --noproxy localhost, 
         -X POST 
         --data "data=testControl,loadModels,start," 
         http://localhost:8081/sendSocketMessage
    

回答by Vi.

For a more low-level (socket.io-independent) solution you can use my tool websocat.

对于更底层(socket.io-independent)的解决方案,你可以使用我的工具websocat

Example session for communicating with socket.io's chat example:

socket.io 的聊天示例通信的示例会话:

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

>表示要键入的消息。

回答by Rjk

You can use iocatcli client from npm which is based on Socket.IO.

您可以使用基于 Socket.IO 的 npm iocatcli 客户端。

$ iocat --socketio ws://127.0.0.1:8081
< Hello there