Web套接字没有'不能在iOS 6.1中使用Chrome和Safari
Web Sockets doesn't work on Chrome and Safari in iOS 6.1
我编写了一个非常基本的web程序,该程序连接到NodeJS web服务器,该服务器从浏览器将文本打印到控制台上。
在电脑上一切都很好,但当我试图从iPhone连接到网络套接字服务器时,它没有连接到它(它连接到http服务器,但没有连接到ws)
SERVER(节点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;
}
}
});
});
我知道这两个浏览器都支持网络套接字,看不出我做错了什么。有人能看到它出了什么问题吗?
您正在使用环回地址127.0.0.1而不是服务器地址打开客户端上的套接字。
var connection = new WebSocket('ws://127.0.0.1:1337');
相关文章:
- jQuery Datepicker可以在Safari中工作,但不能在FF或Chrome中工作
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- JS代码在jsbin中有效,在jsfiddle或Chrome/Safari中无效
- jQuery$.post可以在chrome、safari中工作,但不能在FF中工作(声明成功回调函数未定义)
- 在safari和chrome中,js图像加载产生了奇怪的结果
- 元素意外地隐藏在Safari的某些实例中,总是出现在Firefox、Chrome中
- JS适用于Firefox和Safari,但不适用于Chrome.此处'是我的网站
- 在不支持的浏览器中替代IndexedDB?Safari/iOS Safari Chrome
- 使用内联块设置的 Safari/Chrome 重叠元素
- 桌面网络工具包(safari / chrome)中具有绝对定位元素的奇怪渲染错误
- 在 Safari/Chrome 中,如何在 ready() 和 focus() 上触发函数 A,在 blur() 上触发
- $(“#id选项”).hide();不适用于safari/chrome
- jQuery offset()在Safari/Chrome iOS中不起作用
- 动画gif获胜't在Safari Chrome或Firefox中循环
- 移动Safari/Chrome上Angular应用程序的键盘严重滞后
- jQuery问题(Safari/Chrome)与平滑滚动
- jQuery只适用于firefox或safari/chrome控制台
- 编码浏览器扩展,插件,Firefox, Safari, Chrome等…这是可能的吗?
- JQuery -适用于Firefox,但不适用于Safari, Chrome或Mobile
- AngularJS ng-model在Firefox/Safari/Chrome中