socket.io没有从服务器接收消息

时间:2015-04-03 17:49:22

标签: node.js sockets websocket socket.io

很简单:

我想连接到我的服务器,然后在表单中写一些东西,点击提交,在服务器端解析它,然后发回一个确认。

除最后一条确认消息外,一切正常。它似乎没有到达客户端。该函数永远不会执行,我不知道为什么

服务器:

var express = require('express');
var app = express();
var path = require('path');
var httpServer = require('http').Server(app);
var ioServer = require('socket.io')(httpServer);

function saveConfigToFile(config) {

    if (config) {
        var data = JSON.stringify(config);

        fs.writeFile('./public/config.json', data, function(err) {
            if (err) {
                console.log('[ERROR] Error while saving config file!');
                console.log(err.message);
                return;
            }
            console.log('Config file updated');
            ioServer.emit('info', {msg:'config_saved'});
        })
    }
}

客户端:

socket = new io.connect();

socket.on('connect', function() {
    console.log("connected");
});

socket.on('info', function(data) {
    console.log("check");
    console.log(data.toString());
});

服务器控制台说"配置文件已更新",如果我点击保存。

浏览器控制台说"已连接" (在页面加载时)。

但是"检查" (或消息/数据)永远不会打印......可能是什么原因?

0 个答案:

没有答案
相关问题