node.js socket.io 简单聊天 [英] node.js socket.io simple chat

查看:24
本文介绍了node.js socket.io 简单聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始玩 node.js,和大家一样,我想聊聊.

I'm starting playing with node.js and as everybody, I want do a chat.

例如,我的想法是在端口 9090 中运行带有 socket.io 的 node.js,在端口 8080 中运行我的客户端 html.我的 html 客户端将独立提供.

My idea is run node.js with socket.io in the port 9090, for example, and my client html in the port 8080. My html client will be served independent.

我的服务器:

var sys = require('sys');
var express = require('express');
var io = require('socket.io');

var app = express.createServer();

app.listen(8080);

var socket = io.listen(app);

socket.on('connection', function (client) {
    client.on('message', function (msg) {
        socket.broadcast(msg);
    });
    client.on('disconnect', function () {
    });
});

我的客户:

<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
        <script src="http://cdn.socket.io/stable/socket.io.js"></script>
        <script>
            $(document).ready(function () {
                var socket = new io.Socket("localhost", {port: 8080});

                socket.on('connect', function () {
                    socket.send('A client connected.');
                });
                socket.on('message', function (message) {
                    $('div#messages').append($('<p>'), message);
                });
                socket.on('disconnect', function () {
                    console.log('disconnected');
                });
                socket.connect();

                $('input').keydown(function (event) {
                    if(event.keyCode === 13) {
                        socket.send($('input').val());
                        $('input').val('');
                    }
                });
            });
        </script>
    </head>
    <body>
        <input type="text" style="width: 300px;" />
        <div id="messages" style="border:solid 1px #000;">&nbsp;</div>
    </body>
</html>

我使用 node.js v0.4.10 在 ubuntu 11.04 中运行.

I'm running in ubuntu 11.04 with node.js v0.4.10.

服务器工作正常,但客户端无法连接,在谷歌浏览器的console.log中我收到此消息:XMLHttpRequest 无法加载 http://localhost:8080/socket.io/xhr-polling//1311465961485.Access-Control-Allow-Origin 不允许 Origin http://localhost.

The server works fine, but the client can't do connection, in the console.log on google Chrome I received this message: XMLHttpRequest cannot load http://localhost:8080/socket.io/xhr-polling//1311465961485. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

server.js 位于/var/www/cliente/chat/public 的文件夹中.

The server.js is in a folder in /var/www/cliente/chat/public.

有什么问题吗?

推荐答案

您的客户端代码实际上并未如您所愿从端口 8080 提供服务.

Your client code is not actually being served from port 8080 as you want.

var sys = require('sys');
var express = require('express');
var io = require('socket.io');

var app = express.createServer();
app.listen(8080);
app.use(express.static(__dirname));

app.get('/', function(req, res){
    res.render('index.html', { title: 'Chat' });
});

var socket = io.listen(app);

socket.on('connection', function (client) {
    client.on('message', function (msg) {
        socket.broadcast(msg);
    });
    client.on('disconnect', function () {
    });
});

这应该可以修复您的 Access-Control-Allow-Origin 错误.执行 node server.js 并连接到 http://localhost:8080.一些附加说明:

This should fix your Access-Control-Allow-Origin errors. Execute node server.js and connect to http://localhost:8080. A couple additional notes:

  1. 确保您已安装 socket.io 0.6.x,因为这是您在 html 文件中包含的内容.0.7.x 向后不兼容.

  1. Make sure you have installed socket.io 0.6.x since that's what you are including in your html file. 0.7.x is backwards incompatible.

使用此配置,您将在为页面提供服务的同一端口上运行 socket.io(而不是 9090).

With this configuration you'll be running socket.io on the same port you are serving your page from (as opposed to 9090).

这篇关于node.js socket.io 简单聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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