Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Socket.io发出不';行不通_Javascript_Html_Node.js_Socket.io - Fatal编程技术网

Javascript Socket.io发出不';行不通

Javascript Socket.io发出不';行不通,javascript,html,node.js,socket.io,Javascript,Html,Node.js,Socket.io,我是NodeJS的新手,我刚开始用Express和Socket.io创建一个简单的聊天应用程序,但是“message”发出部分不工作(Socket.on('connect')工作!)。警报“OK”工作正常,但console.log没有任何作用 这是我的密码: App.js var express=require('express'); var-app=express(); var server=require('http')。createServer(应用程序); var io=require

我是NodeJS的新手,我刚开始用Express和Socket.io创建一个简单的聊天应用程序,但是“message”发出部分不工作(Socket.on('connect')工作!)。警报“OK”工作正常,但console.log没有任何作用

这是我的密码:

App.js

var express=require('express');
var-app=express();
var server=require('http')。createServer(应用程序);
var io=require('socket.io')(服务器);
app.get('/',函数(req,res,next){
res.sendFile(uu dirname+'/index.html');
});
app.get('/test',函数(req,res,next){
res.render(“你好”);
});
服务器监听(4200);
io.on('连接',功能(客户端){
//var address=io.handshake.address;
log('Ein neuer Client hat sich zum Chat verbenden!IP:')
});
io.on('sendmsg',函数(数据){
控制台日志(数据);

});您在单个
插座上收听。将侦听器从
io
移动到
client

io.on('connection', function(client) {
   // var address = io.handshake.address;
    console.log('Ein neuer Client hat sich zum Chat verbunden! IP: ')

    client.on('sendmsg', function(data) {
        console.log(data);
    });
});

您可以在单个
插座上收听。将侦听器从
io
移动到
client

io.on('connection', function(client) {
   // var address = io.handshake.address;
    console.log('Ein neuer Client hat sich zum Chat verbunden! IP: ')

    client.on('sendmsg', function(data) {
        console.log(data);
    });
});

事件是在套接字(
客户端
)而不是服务器上触发的:

io.on('connection', function(client) {
   // var address = io.handshake.address;
   console.log('Ein neuer Client hat sich zum Chat verbunden! IP: ');
   client.on("sendmsg", console.log);
});

事件是在套接字(
客户端
)而不是服务器上触发的:

io.on('connection', function(client) {
   // var address = io.handshake.address;
   console.log('Ein neuer Client hat sich zum Chat verbunden! IP: ');
   client.on("sendmsg", console.log);
});

您应该在on
连接
函数中移动处理程序,如下所示:

io.on('connection', function(client) {
   // var address = io.handshake.address;
    console.log('Ein neuer Client hat sich zum Chat verbunden! IP: ');
    client.on('sendmsg', function(data) { // Move this part inside the connection function
      console.log(data);
    });
});

您应该在on
连接
函数中移动处理程序,如下所示:

io.on('connection', function(client) {
   // var address = io.handshake.address;
    console.log('Ein neuer Client hat sich zum Chat verbunden! IP: ');
    client.on('sendmsg', function(data) { // Move this part inside the connection function
      console.log(data);
    });
});
试着这样做

io.on('connection', function(client) {
    console.log('Ein neuer Client hat sich zum Chat verbunden! IP: ')

    client.on('sendmsg', function(data) {
        console.log(data);
    });
});
试着这样做

io.on('connection', function(client) {
    console.log('Ein neuer Client hat sich zum Chat verbunden! IP: ')

    client.on('sendmsg', function(data) {
        console.log(data);
    });
});

也许这里的例子有帮助:也许这里的例子有帮助: