Node.js WebSocket 服务器
目前,我尝试为我们的数据库周围的新活动创建一个推送服务器实例。当然,您会找到很多有关该主题的信息。
我正在使用: http://static.brandedcode.com/nws-docs/#s6-p1< /a>
使用以下客户端实现:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<title></title>
</head>
<body>
<script type="text/javascript">
(function() {
var webSocket = new io.Socket('ws//test', {
port: 8080
});
webSocket.connect();
webSocket.on('connect',function() {
console.log('Client has connected to the server!');
});
webSocket.on('message',function(data) {
console.log('Received a message from the server!',data);
});
webSocket.on('disconnect',function() {
console.log('The client has disconnected!');
});
window.ws = webSocket;
}());
</script>
</body>
</html>
控制台返回:
Unexpected response code: 404
XMLHttpRequest cannot load http://ws//test:8080/socket.io/xhr-polling//1303822796984. Origin http://test is not allowed by Access-Control-Allow-Origin.
1303822796984GET http://ws//test:8080/socket.io/xhr-polling//1303822796984 undefined (undefined)
我不知道问题所在。
感谢您的帮助。
问候!
currently i try to create a push server instance for new activities around our database. Of course, you find a lot of information about this topic.
I'm using:
http://static.brandedcode.com/nws-docs/#s6-p1
With the following client implementation:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<title></title>
</head>
<body>
<script type="text/javascript">
(function() {
var webSocket = new io.Socket('ws//test', {
port: 8080
});
webSocket.connect();
webSocket.on('connect',function() {
console.log('Client has connected to the server!');
});
webSocket.on('message',function(data) {
console.log('Received a message from the server!',data);
});
webSocket.on('disconnect',function() {
console.log('The client has disconnected!');
});
window.ws = webSocket;
}());
</script>
</body>
</html>
The console returns:
Unexpected response code: 404
XMLHttpRequest cannot load http://ws//test:8080/socket.io/xhr-polling//1303822796984. Origin http://test is not allowed by Access-Control-Allow-Origin.
1303822796984GET http://ws//test:8080/socket.io/xhr-polling//1303822796984 undefined (undefined)
I don't know the problem.
Thanks for your help.
Greets!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试使用 Socket.io 直接连接到 WebSocket 服务器。
如果您仅运行 WebSocket 服务器而不是 Socket.io 服务器,则可以使用普通的 HTML5 API 连接到 websockets。
例如:
您使用什么浏览器?
目前仅 Google Chrome 支持 WebSocket。在其他浏览器中测试将会失败。
You´re trying to connect directly to a WebSocket server using Socket.io.
If you are running only a WebSocket server and not the Socket.io server, the you can use the normal HTML5 API to connect to websockets.
for example:
What browser are you using?
WebSockets are currently only supported by Google Chrome. Tests in other browsers will fail.
您可能需要
'ws://push.xxx.binder.test'
而不是'ws//push.xxx.binder.test'
(缺少冒号)。You probably wanted
'ws://push.xxx.binder.test'
instead of'ws//push.xxx.binder.test'
(missing colon).更改
为
You no need to add prefix for your domain for socket.io (尤其是斜杠前没有冒号)。另外 var webSocket 也不是一个好的命名 - socket.io 不仅可以使用 websockets (即使在你的错误中它也使用 xhr-poliing )
change
to
You no need to add prefix for your domain for socket.io (especially without colon before slashes). Also
var webSocket
isn't good naming - socket.io can use not onlywebsockets
(even in your errors it usingxhr-poliing
)