[英]Why doesn't socket.emit emit a package from client to server?
I'm trying to set up a sign in system for my server, but the socket.emit function doesn't seem to be working. 我正在尝试为服务器设置登录系统,但是socket.emit函数似乎无法正常工作。 I'm using socket.io and express.
我正在使用socket.io和表达。
I made sure that there were no errors, there were no errors according to the Chrome developer tab or the command prompt which I'm using as a console for the server. 我确保没有错误,根据我用作服务器控制台的Chrome开发人员标签或命令提示符,没有错误。
var io = require('socket.io')(serv, {});
var socket = io();
socket.on('signIn', function (data) {
console.log('Sign In Request')
});
// client
signInButton.onclick = function () {
socket.emit('signIn', {
username: signInUsername.value,
password: signInPassword.value
});
When I click the button, nothing gets logged to console. 当我单击按钮时,没有任何内容记录到控制台。
I expect the message "Sign In Request" to be logged to console when I click on the button (signInButton). 我希望在单击按钮(signInButton)时将消息“登录请求”记录到控制台。 They are all defined, and I don't know why it doesn't.
它们都已定义,我不知道为什么没有定义。
When writing server-side socket event handling, you need to interact with specific socket objects, after you detect their connection. 编写服务器端套接字事件处理时,需要在检测到特定套接字对象的连接后与它们进行交互。 Like so:
像这样:
// server
const SocketIO = require('socket.io');
const io = SocketIO(serv);
io.on('connection', (socket) => {
socket.on('signIn', (obj) => {
// obj.username / obj.password
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.