0

I wrote a very basic web program that connects to a Node JS web server which print text on to a console thats from the browser.

All good on the computer but when I try to connect to the web socket server from my iPhone, it doesn't connect to it (It connects to the http server but not to the ws)

SERVER (Node JS)

app.get ('/', function(req, res){ fs.readFile('ws.html', 'utf8', function(err, text){ res.send(text); }); }); server.listen(1337, function (){ console.log((new Date()) + " Server is listening on port 1337... "); }); //creating the websocket server websock = new WebSocketServer({ httpServer: server }); //WebSocket Server websock.on('request', function(request) { console.log((new Date()) + ' Connection from origin ' + request.origin + '.'); var connection = request.accept(null, request.origin); var index = clients.push(connection) - 1; console.log((new Date()) + ' Connection accepted.'); //Incoming message handling connection.on('message', function(message) { console.log('Client Says: ' + message.utf8Data); }); connection.on('close', function (connection){ //close connection }); }); 

Client Script $(function () {

var content = $('#content'); var input = $('#input'); var status = $('#status'); window.WebSocket = window.WebSocket || window.MozWebSocket; if (!window.WebSocket) { content.html($('<p>', { text: 'Browser doesn\'t ' + 'support WebSockets.'} )); input.hide(); $('span').hide(); return; } var connection = new WebSocket('ws://127.0.0.1:1337'); connection.onopen = function () { input.removeAttr('disabled'); status.text('Send a Message:'); }; connection.onerror = function (error) { content.html($('<p>', { text: 'Connection Error' } )); }; connection.onmessage = function (message) { // try to decode json try { var json = JSON.parse(message.data); } catch (e) { console.log('Invalid Message Text: ', message.data); return; } // handle incoming message connection.send($('#input').val()); }; input.keydown(function(e) { if (e.keyCode === 13) { var msg = $(this).val(); if (!msg) { return; } connection.send(msg); $(this).val(''); if (myName === false) { myName = msg; } } }); }); 

I know those 2 browsers are supporting web sockets, cannot see what I'm doing wrong. Can anyone see what wrong with it.

1
  • 3
    Are you running node on the jailbroken iOS? You are using 127.0.0.1 as the server address which would imply you are looping back to the iPhone and not to your server. Commented Jul 22, 2013 at 23:36

1 Answer 1

1

You are opening the socket on the client using the loopback address 127.0.0.1 instead of the address of your server.

var connection = new WebSocket('ws://127.0.0.1:1337'); 
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, I really didn't see that just fixed it with var connection = new WebSocket('ws://'+document.domain+':1337');. Thanx!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.