Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to enable tcp, http and websocket all using the same port?
    primarykey
    data
    text
    <p>I am trying to enable <a href="http://nodejs.org/api/net" rel="noreferrer">tcp</a>, <a href="http://nodejs.org/api/http.html" rel="noreferrer">http</a> and <a href="https://github.com/LearnBoost/websocket.io" rel="noreferrer">websocket.io</a> communication on the same port. I started out with the tcp server (part above //// line), it worked. Then I ran the <a href="https://github.com/LearnBoost/websocket.io/blob/master/examples/echo/server.js" rel="noreferrer">echo server example found on websocket.io</a> (part below //// line), it also worked. But when I try to merge them together, tcp doesn't work anymore. </p> <p>SO, is it possible to enable tcp, http and websockets all using the same port? Or do I have to listen on another port for tcp connections?</p> <pre><code>var net = require('net'); var http = require('http'); var wsio = require('websocket.io'); var conn = []; var server = net.createServer(function(client) {//'connection' listener var info = { remote : client.remoteAddress + ':' + client.remotePort }; var i = conn.push(info) - 1; console.log('[conn] ' + conn[i].remote); client.on('end', function() { console.log('[disc] ' + conn[i].remote); }); client.on('data', function(msg) { console.log('[data] ' + conn[i].remote + ' ' + msg.toString()); }); client.write('hello\r\n'); }); server.listen(8080); /////////////////////////////////////////////////////////// var hs = http.createServer(function(req, res) { res.writeHead(200, { 'Content-Type' : 'text/html' }); res.end(['&lt;script&gt;', "var ws = new WebSocket('ws://127.0.0.1:8080');", 'ws.onmessage = function (data) { ws.send(data); };', '&lt;/script&gt;'].join('')); }); hs.listen(server); var ws = wsio.attach(hs); var i = 0, last; ws.on('connection', function(client) { var id = ++i, last console.log('Client %d connected', id); function ping() { client.send('ping!'); if (last) console.log('Latency for client %d: %d ', id, Date.now() - last); last = Date.now(); }; ping(); client.on('message', ping); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload