javascript - Connect two socket.io clients together (Establish a socket to socket, cross browser connection) -
i'd know if there's way establish p2p connection between 2 browsers using socket.io-client (but i'm willing use else may trick).
both browsers connected node.js app serving http requests express, stores both clients's ip addresses (and ports when running locally). i'd add third connection links both clients (let's call them , b) directly, messages/data go straight 1 client another, without transiting through node.js server.
is feasible? if so, how?
so far, i've tried connecting 2 clients (let's call them , b) following code:
client a:
a_to_server_socket = io(); a_to_server_socket.on('p2p', function(address_b){ a_to_b_socket = io(address_b); // instantiates new socket a_to_b_socket.on('connect', function() { console.log('connected!'); }); });
i'm not sure code client b. i've tried:
repeat above code b, using b's own address (to override default of connecting server)
repeat above code b, time using a's address
having b_to_server_socket listen new connect event
however regardless of b's code, when running a's code i'm confronted "cross-origin request blocked" error on firefox, or "failed load resource: net::err_connection_refused" followed "net::err_connection_refused" on chrome.
any hints towards solution, or insights better understanding problem , how sockets work welcome.
i'll try summarize comments answer.
in tcp, connection made when 1 endpoint connects endpoint b. connect endpoint b, host must "listening" incoming connections originating other hosts. in typical web request, browser establishes tcp connection web server , works because web server listening incoming requests on specific port , when 1 of requests comes in, accepts incoming request establish active tcp connection. so, must have 1 side initiating request , side listening request.
for various security reasons, browsers don't "listen" incoming connections can't connect directly them. allow connect outbound other listening agent on internet. so, if browser never listens incoming websocket connection, can't establish true peer-to-peer (e.g. browser-to-browser) websocket connection.
furthermore, tcp designed can't have 2 browsers both connect common server , somehow have server connect pipelines such 2 browser wired directly each other. tcp doesn't work way. possible have agent in middle forwarding packets 1 client via separate connection each (that's how chat applications work), agent in middle can't plug 2 tcp connections such packets go directly 1 client other (and no longer go through server) fireman connect 2 firehoses. tcp doens't work way. might possible have complicated scheme involved rewriting packet headers packet sent endpoint forwarded endpoint b , looked came server instead, still involves server middleman or proxy.
the usual way solve problem let each browser connect common server , have server act middleman or traffic cop, forwarding packets 1 browser another.
existing p2p applications (outside of browsers) work having each client listen incoming connections (act server) client can connect directly them. p2p more complicated because 1 needs have means of discovering ip address connect (since clients typically aren't in dns) , there firewalls in way need cooperation between 2 ends in order make firewall allow incoming connection. but, alas, capability of listening incoming connection not browser plain javascript allow do.
Comments
Post a Comment