我正在尝试使用 websocket 连接到 node.js 服务器。我正在开发一个 Phonegap 应用程序。当我尝试使用 iOS 6.x 模拟器连接时,它通过 websocket 完美地连接到服务器。但是使用 iOS 5.x 模拟器它没有连接。我看到当我运行代码时,它立即使用 ios 5.x 模拟器调用 onclose 方法。我正在使用 node.js 版本 0.10.17,也尝试使用 0.10.7。
我为 node.js 安装了 websocket 模块,这是我的客户端连接代码
function openConnection()
{
// if user is running mozilla then use it's built-in WebSocket
window.WebSocket = window.WebSocket || window.MozWebSocket;
// if browser doesn't support WebSocket, just show some notification and exit
if (!window.WebSocket)
{
alert("Sorry your browser does not support websockets");
return;
}
// new socket
connection = new WebSocket('ws://127.0.0.1:1337'); //change the ip address for online
// push a message after the connection is established.
connection.onopen = function()
{
connection.send(sender_id); //change this iuserID in real case.
//connection.send('Sample1');
var message ="receive_offline_messages";
var message_to_be_sent = JSON.stringify({recevieruserid:sender_id,message:message});
connection.send(message_to_be_sent);
//Is the receiver online / offline
var message ="get_delivery_report";
var message_to_be_sent = JSON.stringify({message_sender_id:sender_id,message:message});
connection.send(message_to_be_sent);
connected_to_the_server=1;
get_unsent_message();
change_unsent_message_status();
};
}
.....