Socket.IO基本示例不起作用 [英] Socket.IO basic example not working

查看:224
本文介绍了Socket.IO基本示例不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm 100% new to Socket.IO and have just installed it. I was trying to follow some examples, and can get the server side running but I can't seem to get the client side connected.

以下是我的server.js:

The following is 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.');
    });

});

这是我的index.html

This is my 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>

当我执行node.js节点时,它表示socket.io已启动。

When I do node server.js it indicates that socket.io is started.

当我加载index.html时会出现一行显示debug - served static /socket.io.js
但没有别的,没有控制台消息或其他行。

When I load the index.html a line comes up indicating "debug - served static /socket.io.js" But nothing else, no console messages or other lines.

非常感谢您提供的任何指导。正如我所说的那样我100%绿色,所以如果你能尽可能地分解它我也会很感激。

Any guidance you could provide would be very much appreciated. As I said I'm 100% green with this so if you could break it down as much as possible I'd also appreciate it.

谢谢

推荐答案

同源政策 localhost:8090 localhost 不是同一个来源(不同的端口),所以 localhost / index.html 无法连接到localhost:8090上的socket.io。

In same origin policy, localhost:8090 and localhost are not the same origin (different port), so localhost/index.html cannot connect to socket.io on localhost:8090.

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

One option is to make index.html on localhost:8090 (see code below). Then you just put "index.html" into the same directory of your server script, start the server and type localhost:8090 in your browser.

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);
        }
    });
});

这篇关于Socket.IO基本示例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆