似乎无法让socket io显示任何发出

时间:2015-09-04 08:48:57

标签: node.js sockets express io

我一直在试图找出为什么我无法在我的终端中显示任何发出的东西,似乎一切都运行良好....除了看到发射。这是我的代码

var express = require('express');
var path = require('path');

// Create a new Express application
var app = express();

var views = path.join(process.cwd(), 'views');
app.use("/static", express.static("public"));

// Create an http server with Node's HTTP module. 
// Pass it the Express application, and listen on port 3000.
var server = require('http').createServer(app).listen(3000, function() {
    console.log('listening on port ' + 3000)
});

// Instantiate Socket.IO hand have it listen on the Express/HTTP server
var io = require('socket.io')(server);

var game = require('./game');

app.get('/', function(req,res) {
    res.sendFile(path.join(views, 'index.html'));
});

io.on('connect', function(socket) {
    io.emit('connection', { message: "You are connected!" });
    game.initGame(io, socket);
    socket.emit('connected', { message: "You are connected!" });
    io.sockets.emit('test', 'test')
});

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

不会自动打印发射。 socket.emit会将消息发送回客户端,而不是终端。使用console.log("whatever")打印到终端:

io.on('connect', function(socket) {
    console.log('Client connected');
    socket.on('test', function(data) {
        console.log("Got message of type 'test'containing data:", data);
    });
});