Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 不使用socket.io的简单nodeJS示例_Node.js_Socket.io - Fatal编程技术网

Node.js 不使用socket.io的简单nodeJS示例

Node.js 不使用socket.io的简单nodeJS示例,node.js,socket.io,Node.js,Socket.io,为了使这个简单的示例能够使用socket.io工作,我们已经奋斗了一整天。我最初在Windows7上试用过Cygwin。此后,他们也在OSX上进行了尝试,取得了相同的结果 运行脚本时,会显示以下内容 2 May 20:57:47 - socket.io ready - accepting connections 但是访问index.html页面并不表明客户端已经连接 index.html <html> <head> <script type="text/javas

为了使这个简单的示例能够使用socket.io工作,我们已经奋斗了一整天。我最初在Windows7上试用过Cygwin。此后,他们也在OSX上进行了尝试,取得了相同的结果

运行脚本时,会显示以下内容

2 May 20:57:47 - socket.io ready - accepting connections
但是访问index.html页面并不表明客户端已经连接

index.html

<html>
<head>
<script type="text/javascript" src="socket.io.js"></script> 
<script type="text/javascript"> 
    var socket = new io.Socket('localhost',{'port':8090});

    socket.connect();

    socket.on('connect', function(){
        console.log('connected');
        socket.send('hi!'); 
    });

    socket.on('message', function(data){ 
        console.log('message recived: ' + data);
    });

    socket.on('disconnect', function(){
        console.log('disconected');
    });
</script> 
</head>
<body></body>
</html>

var socket=new io.socket('localhost',{'port':8090});
socket.connect();
socket.on('connect',function(){
console.log('connected');
socket.send('hi!');
});
socket.on('message',函数(数据){
console.log('收到的消息:'+数据);
});
socket.on('disconnect',function()){
console.log('disconnected');
});
server.js

var http = require('http'), io = require('socket.io'),

server = http.createServer(function(req, res){ 
    res.writeHead(200, {'Content-Type': 'text/html'}); 
    res.end('<h1>Hello world</h1>'); 
});
server.listen(8090);

var socket = io.listen(server); 
socket.on('connection', function(client){ 
    console.log('client connected');

    client.on('message', function(){ 
        console.log('message arrive');
        client.send('some message');
    });

    client.on('disconnect', function(){
        console.log('connection closed');
    });
});
var http=require('http'),io=require('socket.io'),
server=http.createServer(函数(req,res){
res.writeHead(200,{'Content-Type':'text/html'});
res.end(“你好,世界”);
});
监听服务器(8090);
var socket=io.listen(服务器);
socket.on('connection',函数(客户端){
log(“客户端连接”);
on('message',function(){
log(“消息到达”);
client.send('somemessage');
});
client.on('disconnect',function()){
console.log(“连接关闭”);
});
});

你知道我做错了什么吗?没有显示任何控制台消息。值得注意的是,当我使用Firebug查看index.html页面时,没有嵌入脚本,这很奇怪。不确定是什么原因造成了这种情况。

您没有在index.html文件中正确加载socket.io库。试试这个:

<script type="text/javascript" src="http://localhost:8090/socket.io/socket.io.js"></script> 

您没有提供socket.io.js(或闪存文件)

我建议使用CDN:

或者,也可以使用为socket.io.js文件提供服务

编辑:

实际上,仔细看,您也没有提供index.html 同样,express也可以工作,但对于一个简单的示例:

var fs = require('fs');
var index = fs.readFileSync('index.html');
//note the readFileSync is done only in the first tic
.
.
.
res.writeHead(200, {'Content-Type': 'text/html'}); 
res.end(index); 

是,并注释以下行:

// server.listen(8090);

在客户端将其用作路径

<script type="text/javascript" src="/socket.io/socket.io.js"></script> 


使用CDN版本实际上存在一些问题。。对我来说,WebSocket不起作用。。我建议坚持本地版本啊,就是这样!我最初使用的是express,但返回http以隔离问题。我回到express,开始使用app.configure(“开发”)功能。谢谢你的帮助+1、正确且比从远程主机获取脚本更好的方法。