简体   繁体   English

WebSocket在此浏览器中连接另一个websocket

[英]WebSocket connect another websocket in this browser

I have my browser connecting to system hardware application at localhost using websocket. 我的浏览器使用websocket连接到本地主机上的系统硬件应用程序。 Sometimes this hardware application disabled, so I want to emulate this hardware application directly in the browser by creating another websocket. 有时,此硬件应用程序已禁用,因此我想通过创建另一个Websocket在浏览器中直接模拟此硬件应用程序。 How can I do this? 我怎样才能做到这一点?

I tried to emulate it by creating two websockets simultaneously, something like this: 我试图通过同时创建两个websocket来模拟它,如下所示:

socket1 = new WebSocket('ws://localhost:8080')
socket2 = new WebSocket('ws://localhost:8080')

But this doesn't work. 但这是行不通的。 How can I establish this connection? 如何建立此连接?

I'd suggest using a mocking framework to mock the server in this situation. 我建议在这种情况下使用模拟框架来模拟服务器。 I have succesfully used mock-socket to do exactly this, eg. 我已经成功地使用mock-socket来做到这一点,例如。 (quoting example from README.md): (引自README.md的示例):

var mockServer = new MockServer('ws://localhost:8080');
mockServer.on('connection', function(server) {
    server.on('message', function(data) {
        server.send('hello world!');
    } );
} );

Hope that helps. 希望能有所帮助。

Cheers, 干杯,

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

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