无法连接到服务器

无法连接到服务器

本文介绍了nodejs socket.io 无法连接到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整个下午都在关注 node.js 和 socket.io 示例,我正在尝试拼凑一个简单的页面,该页面将告诉我我连接到服务器的用户数量.

我已阅读 http://socket.io/ 上的文档以及一些教程/问题这里概述了我正在尝试做的事情.我还发现 create simple node js server and client 没有帮我.

版本信息:

node.js - 0.6.15
快递 - 3.0.0alpha1
socket.io - 0.9.5(socket.io-client 是同一个版本,但是没有找到资源...见评论)
ejs - 0.7.1

这是我的服务器代码:

var express = require('express'),配置 = {端口:4000,主机名:'本地主机'};var server = module.exports = express.createServer();/* 服务器配置 */server.use(express.cookieParser('键盘独角兽'));server.use(express.bodyParser());server.use(express.methodOverride());server.use(express.session({secret: '键盘独角兽'}));server.engine('.html', require('ejs').__express);server.set('views', __dirname + '/lib/views');server.set('view options', { layout: false });server.set('视图引擎', 'html');server.use(server.router);server.use('/', express.static(__dirname + '/lib/assets'));var io = require('socket.io').listen(server);var 连接 = { '长度': 0 };io.sockets.on('connection', function(socket) {socket.__fd = socket.fd;连接[socket.__fd]=socket.remoteAddress;++connections.length;console.log('用户已连接!总共有'+connections.length +'在线用户.');return socket.on('disconnect',function(){删除连接[socket.__fd];--connections.length;console.log('用户已断开连接!总共有' + 连接数.length + ' 个用户在线.');});});server.get('/', function( req, res ) {res.render('索引', {'page_title': '示例应用程序','number_of_connections':connections.length});});server.listen(config.port, config.hostname);

这是我的客户端代码:

<html lang="zh-cn"><头><meta charset="utf-8"><title><%= page_title %></title><身体><div>总共有<%= number_of_connections %>用户已连接.

<script src="http://cdn.socket.io/stable/socket.io.js"></script><script type="text/javascript">var socket = new io.Socket('localhost',{'port':4000});套接字连接();socket.on('connect', function() {console.log('连接');});

这是我运行服务器并与客户端连接时发生的情况.

$ node server.js信息 - socket.io 已启动

然后,当我在 Web 浏览器上导航到 localhost:4000 时,我会看到带有0"(number_of_connections)的页面.此外,我在服务器的终端中看不到任何内容(on.('connection' 从未被击中).

在客户端上,一两秒后,我开始收到大量错误(如果我让控制台打开几秒钟,它会导致页面崩溃),请参见下图:

请提供有关从哪里开始调试的任何帮助,我们将不胜感激!我只是想让这个基本示例运行起来,这样我就可以开始玩/理解 nodejs 和 socket.io!

解决方案

问题是因为 Express 现在是一个函数.

您需要执行以下操作:

var express = require('express');var app = express();var server = app.listen(3000);var io = require('socket.io').listen(server);

I've been staring at node.js and socket.io examples all afternoon and i'm trying to piece together a simple page that will tell me how many users I have connected to the server.

I have read the documentation at http://socket.io/ as well as a few tutorials / questions here that outline exactly what i'm trying to do. I have also found create simple node js server and client which does not help me.

Version Information:

Here is my server code:

var express = require('express'),
    config = {
        port: 4000,
        hostname: 'localhost'
    };

var server = module.exports = express.createServer();
    /* server configuration */
    server.use(express.cookieParser('keyboard unicorn'));
    server.use(express.bodyParser());
    server.use(express.methodOverride());
    server.use(express.session({ secret: 'keyboard unicorn' }));
    server.engine('.html', require('ejs').__express);
    server.set('views', __dirname + '/lib/views');
    server.set('view options', { layout: false });
    server.set('view engine', 'html');
    server.use(server.router);
    server.use('/', express.static(__dirname + '/lib/assets'));

var io = require('socket.io').listen(server);

var connections = { 'length': 0 };

io.sockets.on('connection', function(socket) {
    socket.__fd = socket.fd;
    connections[socket.__fd]=socket.remoteAddress;
    ++connections.length;
    console.log('user connected! There are a total of ' + connections.length + ' users online.');
    return socket.on('disconnect',function(){
        delete conns[socket.__fd];
        --connections.length;
        console.log('user disconnected! There are a total of ' + connections.length + ' users remaining online.');
    });
});

server.get('/', function( req, res ) {
    res.render('index', {
        'page_title': 'sample application',
        'number_of_connections': connections.length
    });
});

server.listen(config.port, config.hostname);

Here is my client code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><%= page_title %></title>
</head>
<body>
<div>There is a total of <%= number_of_connections %> user(s) connected.</div>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script type="text/javascript">
var socket = new io.Socket('localhost',{'port':4000});
socket.connect();
socket.on('connect', function() {
    console.log('connected');
});
</script>
</body>
</html>

Here is what is happening when I run the server, and connect with the client.

$ node server.js
   info  - socket.io started

then when I navigate to localhost:4000 on my web browser I get the page with '0' (number_of_connections). Also, I see nothing in the terminal for the server (the on.('connection' is never hit).

On the client, after a second or two, I start to get a massive number of errors (if I leave the console open for a few seconds it crashes the page) see image below:

Please, any help with where to start for debugging this would be appreciated! I just want to get this basic example up and running so I can start playing with / understanding nodejs and socket.io!

解决方案

The problem is because of Express now being a function.

You need to do the following:

var express = require('express');
var app = express();
var server = app.listen(3000);

var io = require('socket.io').listen(server);

这篇关于nodejs socket.io 无法连接到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 12:05