Javascript Socket.IO基本示例不工作

Javascript Socket.IO基本示例不工作,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我对Socket.IO是100%的新手,刚刚安装了它。 我试图按照一些例子,可以让服务器端运行,但我似乎无法连接客户端 以下是my 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('

我对Socket.IO是100%的新手,刚刚安装了它。 我试图按照一些例子,可以让服务器端运行,但我似乎无法连接客户端

以下是my 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.');
        client.send('Lorem ipsum dolor sit amet');
    });

    client.on('disconnect', function(){
        console.log('Disconnected.');
    });

});
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('Client connected');
on('message',function(){
console.log('Message');
客户发送(“Lorem ipsum dolor sit amet”);
});
client.on('disconnect',function()){
console.log('Disconnected');
});
});
这是我的index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Socket Example</title>
    <base href="/" />
    <meta charset="UTF-8" />
    <script src="http://localhost:8090/socket.io/socket.io.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
</head><body>
<script type="text/javascript"> 
$(document).ready(function(){

    var socket = new io.Socket('localhost', { 'port': 8090 });

    socket.connect();
    console.log("Connecting.");

    socket.on('connect', function () {
        console.log("Connected.");
    });

    socket.on('message', function (msg) {
        console.log("Message: " + msg + ".");
    });

    socket.on('disconnect', function () {
        console.log("Disconnected.");
    });

});
</script> 
</body></html>

套接字示例
$(文档).ready(函数(){
var socket=new io.socket('localhost',{'port':8090});
socket.connect();
console.log(“连接”);
socket.on('connect',function(){
console.log(“已连接”);
});
socket.on('message',函数(msg){
日志(“消息:“+msg+”);
});
socket.on('disconnect',function(){
console.log(“断开连接”);
});
});
当我执行node server.js时,它表示socket.io已启动

加载index.html时,会出现一行,指示“debug-serviced static/socket.io.js” 但没有其他内容,没有控制台消息或其他线路

如果您能提供任何指导,我们将不胜感激。正如我所说,我对这个100%的绿色,所以如果你能尽可能多地分解它,我也会很感激

感谢

在中,
localhost:8090
localhost
不是同一来源(不同端口),因此
localhost/index.html
无法连接到localhost:8090上的socket.io

一个选项是在
localhost:8090
上生成index.html(参见下面的代码)。然后将“index.html”放入服务器脚本的同一目录中,启动服务器并在浏览器中键入
localhost:8090

var fs = require('fs');

server = http.createServer(function(req, res){
    fs.readFile(__dirname + '/index.html', 'utf-8', function(err, data) { // read index.html in local file system
        if (err) {
            res.writeHead(404, {'Content-Type': 'text/html'});
            res.end('<h1>Page Not Found</h1>');
        } else { 
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.end(data);
        }
    });
});
var fs=require('fs');
server=http.createServer(函数(req,res){
在本地文件系统中,fs.readFile(uuu dirname+'/index.html',utf-8',function(err,data){//read index.html
如果(错误){
res.writeHead(404,{'Content-Type':'text/html'});
res.end(“未找到页面”);
}否则{
res.writeHead(200,{'Content-Type':'text/html'});
res.end(数据);
}
});
});

index.html的URL位置是什么?如果它不是以“”启动的,浏览器将阻止您连接到服务器,因为它违反了同源策略。那么,如何构造文件,使您能够在网页上执行简单的聊天脚本呢。。喜欢如何在web服务器上指向domain.com:8090/index.html等。。。那么index.html实际上是打开的吗?它位于localhost/Test/index.html(就像localhost/Test/server.js是服务器javascript一样)。我只是想找出最好的方法来构造文件并使其连接。当我转到localhost:8090时,我会看到Hello World。但当我尝试访问localhost:8090/index.html或通过标准的:80端口地址时,什么都没有。