Javascript Socket.io发出不';行不通
我是NodeJS的新手,我刚开始用Express和Socket.io创建一个简单的聊天应用程序,但是“message”发出部分不工作(Socket.on('connect')工作!)。警报“OK”工作正常,但console.log没有任何作用 这是我的密码: App.jsJavascript 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
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);
});
});
也许这里的例子有帮助:也许这里的例子有帮助: