我編寫了一個非常基本的Web程序,它連接到一個Node JS Web服務器,該服務器將文本打印到來自瀏覽器的控制檯上。Web套接字不適用於iOS 6.1中的Chrome和Safari
所有在電腦上很好,但是當我嘗試連接到從我的iPhone網絡套接字服務器,它不連接到它(它連接到HTTP服務器,而不是到WS)
服務器(節點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
});
});
客戶端腳本 $(函數(){
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;
}
}
});
});
我知道這2個瀏覽器支持網絡套接字,看不到我做錯了什麼。任何人都可以看到它有什麼問題。
你在越獄的iOS上運行節點嗎?您使用127.0.0.1作爲服務器地址,這意味着您將循環回到iPhone而不是您的服務器。 – veritasetratio