我有一个node.js服务器正在运行,当我导航到url时,我可以让它显示“hello world”或twitter feed。Node.js和Web套接字
问题是我无法在node.js实例和另一个页面的客户端上定义的websocket之间发生任何通信。
有没有人有任何想法?
非常感谢。
我有一个node.js服务器正在运行,当我导航到url时,我可以让它显示“hello world”或twitter feed。Node.js和Web套接字
问题是我无法在node.js实例和另一个页面的客户端上定义的websocket之间发生任何通信。
有没有人有任何想法?
非常感谢。
是否在一些代理后面运行node.js?一些代理(例如ngnix)不支持http 1.1,而web 1.1需要http 1.1。
好了,所以我得到了这个工作(我认为)
再次,客户端代码:
<script src="./Socket.IO/socket.io.js"></script>
<script>
io.setPath('./Socket.IO/');
var socket = new io.Socket('jayz.danstanhope.webfactional.com', { 'port': 80 });
socket.on('connect', function() {
alert('connect');
});
socket.on('message', function (msg) {
alert('message' + msg);
});
socket.on('close', function() {
alert('close');
});
socket.on('disconnect', function() {
alert('disconnect');
});
socket.connect();
</script>
服务器端代码:
var sys = require("sys")
, fs = require("fs")
, path = require("path")
, http = require("http");
var io = require('/home/danstanhope/webapps/htdocs/Socket.IO-node');
var server = http.createServer(function (req, res) {
//your normal server code
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('Hello world');
res.end();
});
server.listen(26970);
server = io.listen(server);
server.on('connection', function(client){
sys.log('client connected');
});
当我刷新页面在Chrome中,我可以看到正在Shell中写入的日志。
这是我看到:
[email protected] htdocs]$ node server.js
9 Aug 19:19:37 - socket.io ready - accepting connections
9 Aug 19:19:40 - Initializing client with transport "websocket"
9 Aug 19:19:40 - Client 21789167495444417 connected
9 Aug 19:19:40 - client connected
9 Aug 19:19:40 - Client 21789167495444417 disconnected
唯一的问题现在越来越任何的JavaScript的插座警报火。
任何想法?
感谢, 丹
参见:http://stackoverflow.com/questions/3127864/node-js-websocket-location-issue/3443892#3443892 – Ryan 2010-08-09 21:19:18