javascript - How to set up websocket server listener? - Stack Overflow

I am trying to make a websocket client in a chrome extension that will listen for open websocket server

I am trying to make a websocket client in a chrome extension that will listen for open websocket servers. Once one is open, it will connect and perform its task. How do I set it so that the client will start listening right after disconnection?

This is the code I have to connect, but I have no idea how to make it into a listener.

function connect(host) {
    var ws = new WebSocket(host);

    ws.onopen = function () {
        alert('connected');
        connection = true;
    };

ws.onmessage = function (evt, tab) {  
    if(evt.data == "connect"){
        rpwd = 'hello ws';
        ports[curTabID].postMessage({text: rpwd});
}
};

ws.onclose = function () {
    alert('socket closed');
    connection = false;
    webConnect('ws://localhost:8080');
};
};

I am trying to make a websocket client in a chrome extension that will listen for open websocket servers. Once one is open, it will connect and perform its task. How do I set it so that the client will start listening right after disconnection?

This is the code I have to connect, but I have no idea how to make it into a listener.

function connect(host) {
    var ws = new WebSocket(host);

    ws.onopen = function () {
        alert('connected');
        connection = true;
    };

ws.onmessage = function (evt, tab) {  
    if(evt.data == "connect"){
        rpwd = 'hello ws';
        ports[curTabID].postMessage({text: rpwd});
}
};

ws.onclose = function () {
    alert('socket closed');
    connection = false;
    webConnect('ws://localhost:8080');
};
};
Share asked Jun 5, 2014 at 20:52 Dat_фрикаделькаDat_фрикаделька 611 gold badge1 silver badge10 bronze badges 3
  • What you have in the browser us just a client, it cannot act as a server. – vtortola Commented Jun 5, 2014 at 21:53
  • I know this is much later but the blog post was the next month after, way back in Aug '15: blog.revathskumar./2015/08/… (it also requires Node.js, like socket.io) – JakeJ Commented Jan 18, 2018 at 23:15
  • Basically, the browser's websockets can only listen to a port, but you can run Javascript in Node.js on the same machine that puts up a server on the same port, takes in connections from elsewhere, and serves as the intermediary between the browser and outside clients. – JakeJ Commented Jan 18, 2018 at 23:21
Add a ment  | 

2 Answers 2

Reset to default 2

Unfortunately, you can't easily do it.

1) Standard API, WebSocket, is only a client, nothing you can do about that. The browser is not supposed to be a server.

2) Extensions are supposed to augment the browser functionality, so chrome APIs can possibly help. Unfortunately, in case of extensions the answer is still no.

3) For Chrome apps, however, it is possible. There is a chrome.sockets.tcpServer API, but of course no bundled server implementations. You would have to either implement a web+ws server yourself, or use one of the existing solutions like this one.


That aside, are you sure you really want a WS server? You said it's to "listen for open servers" - how would servers make their presence known? Wouldn't it be more logical to just poll them periodically to see if they are open?

It doesn't seem like a browser has any way of listening for connections using WebSockets. Everything I have read including the documentation for socket.io point to node.js being the only JavaScript way of listening, or providing server sockets.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744925632a4601417.html

相关推荐

  • javascript - How to set up websocket server listener? - Stack Overflow

    I am trying to make a websocket client in a chrome extension that will listen for open websocket server

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信