【问题标题】:using socket.io in conjunction with express将 socket.io 与 express 结合使用
【发布时间】:2014-08-04 13:13:15
【问题描述】:

我正在尝试在用户连接和断开连接时发送基本消息。后来我想来回发送参数。但是使用以下代码,当它们连接/断开连接时,我不会在服务器上打印出消息。

当我用 server.on 替换 io.on 时,我可以在刷新/关闭页面时收到消息“用户已连接”但用户未断开连接。然而,这是有问题的,因为不是每次我刷新它都会记录消息,并且在第一次它有时会多次记录消息。

server.js

var //io      = require('socket.io'),
    http    = require('http'),
    express = require('express');

var PORT = process.env.PORT || 8080,
    HOST = process.env.HOST || 'localhost';

var app = express();
app.use(express.static(__dirname + '/views'));

app.get('/', function (req, res) {
    res.sendfile(__dirname + 'index.html');
});

server = http.Server(app);
//io = io.listen(server);
var io = require('socket.io')(server);

io.on('connection', function(socket){
    console.log('User connected');


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


server.listen(PORT, HOST, null, function() {
    console.log('Server listening on port %d in %s mode', this.address().port, app.settings.env);
});

新代码:

var express = require('express'),
    app = express(),
    http = require('http'),
    server = http.createServer(app),
    io = require('socket.io').listen(server);


var PORT = process.env.PORT || 8080,
    HOST = process.env.HOST || 'localhost';


server.listen(PORT);

app.use(express.static(__dirname + '/views')); //need this for my directory structure.


app.get('/', function (req, res) {
    res.sendfile(__dirname + 'index.html');
}); //Need this to call my page.


io.on('connection', function( client ){
    console.log('Connected');//this does not work.


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

【问题讨论】:

    标签: node.js express socket.io


    【解决方案1】:

    尝试重新配置您的套接字和服务器实例化:

    var express = require('express'),
        app = express(),
        server = require('http').createServer(app),
        io = require('socket.io').listen(server);
    
    server.listen(process.env.PORT || 3000);
    
    io.on('connection', function( client ) {
        console.log(client) //this will now work
    
       client.on('disconnect', function( id ) { 
           console.log('user with ID ' + id + ' has disconnected');
       });
    });
    

    【讨论】:

    • 无法让它工作,我会在几秒钟内发布我的新代码。
    • 啊,我现在明白了。您有 io.on('disconnect'),您将需要使用客户端(或您传递的任何参数)。试试:client.on('disconnect', function() { });
    • “连接”根本没有触发。
    • 您是否在客户端包含了套接字变量? var socket = io.connect() 并确认它工作正常?
    • 没有脚本源链接。
    【解决方案2】:

    首先你需要确定你是否包含了客户端的socket.io文件。

    <script src="/socket.io/socket.io.js"></script>
    

    然后你需要初始化socket连接。

    <script>
     var socket = io();
     </script>
    

    以上更改将在您的 index.html 文件中。

    现在在 server.js 文件中,

    io.sockets.on('connection', function(socket) {
        console.log("User connected");
       socket.on('disconnect', function() {
          console.log('Got disconnect!');
    
       });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-05-21
      • 1970-01-01
      • 2018-06-02
      相关资源
      最近更新 更多