简体   繁体   English

将Perl脚本连接到WebSocket(使用Mojolicious)

[英]Connect perl script to a websocket (using mojolicious)

Here is my issue : I have got an API using mojolicious, an external script perl and a JS file, and I would like to connect them this way: the external script launch a random POST request, if it is a success it have to send the message "Success" throught a websocket. 这是我的问题:我有一个使用mojolicious的API,一个外部脚本perl和一个JS文件,我想以这种方式连接它们:外部脚本会启动一个随机POST请求,如果成功,则必须发送网络套接字中显示“成功”消息。 If an error occures, It will have to send "Error". 如果发生错误,则必须发送“错误”。 The websocket on the API will just relay the message for the JS which will use it. API上的websocket只会中继将使用它的JS消息。

How I imagine the code to be : 我如何想象代码是:

Inside the Mojolicious launcher script: 在Mojolicious启动器脚本中:

websocket '/foo' => sub {
 $self->on(message => sub {
  my ($self, $msg) = @_;
  $self->send($msg);
 });
};

when get a message send it 收到消息后发送

Inside the JS file: 在JS文件中:

var ws = new WebSocket('ws://api/foo'); 
ws.onmessage = function(msg){
 if(msg == "Error") {console.log("got an error")};
 else if(msg == "Success") {console.log("got a success")};
};

So, how can I connect my external script to the websocket, and be able to send "Error" or "Success"? 那么,如何将我的外部脚本连接到Websocket,并能够发送“错误”或“成功”? (This external script has nothing to do with the web server, it s somewhere else, doing something else). (此外部脚本与Web服务器无关,它位于其他位置,正在执行其他操作)。

I understand that you want a plain perl-script who connects to the Mojolicious webserver where you have a websocket endpoint. 我了解您需要一个简单的Perl脚本,该脚本可以连接到具有websocket端点的Mojolicious Web服务器。 The external perl script should connect to the websocket server and send some messages. 外部perl脚本应连接到websocket服务器并发送一些消息。 The websocket server can then redistribute those messages to other clients. 然后,WebSocket服务器可以将这些消息重新分发给其他客户端。

Check my github where you can find the above screnario. 检查我的github,您可以在上面找到以上场景。

Perl Mojolicious websockets Perl Mojolicious网络套接字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM