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

查看:81
本文介绍了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.

服务器工作正常,但客户端无法建立连接,在Google Chrome浏览器的console.log中,我收到以下消息: XMLHttpRequest无法加载 http://localhost:8080/socket.io/xhr-polling//1311465961485 . Access-Control-Allow-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.

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

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天全站免登陆