[英]Socket.io only emitting uppercase events
so I wanted to try out the socket.io library and all the examples work perfectly fine (with lowercase emits).所以我想尝试 socket.io 库,所有示例都运行良好(带有小写字母)。 But when I try to code my one little ping->pong it doesnt emit the events (I can view the message log in firefox network tab).
但是当我尝试编写我的一个小 ping->pong 时,它不会发出事件(我可以在 firefox 网络选项卡中查看消息日志)。
Code Server (Node JS):代码服务器(节点 JS):
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http);
const port = process.env.PORT || 9000;
app.use(express.static(__dirname + "/public"));
io.on("connection", (socket) => {
console.log("Socket connected");
socket.on("ping", () => {
console.log("PING");
socket.emit("pong", {});
});
});
http.listen(port, () => console.log("listening on port " + port));
Code in Browser:浏览器中的代码:
var socket = io();
socket.on("pong", () => {
console.log("recieved PONG");
});
const ping = () => {
socket.emit("ping");
};
document.addEventListener("mousedown", ping, false);
Strangely, this doesn't seem to work, "Socket connected" is printed, but the sockets don't emit anything.奇怪的是,这似乎不起作用,打印了“Socket connected”,但 sockets 不发出任何东西。 If I change the emits and on's from "ping"->"PING" and from "pong"->"PONG" everything works perfectly fine.
如果我从“ping”->“PING”和“pong”->“PONG”更改发射和开启,一切正常。 Im just totally confused to why this is and why the examples can use lowercase emits.
我完全不明白为什么会这样以及为什么这些示例可以使用小写字母。
Since there is nothing related with UPPERCASE/LOWERCASE event names, you may use them as you wish.由于与大写/小写事件名称没有任何关系,您可以随意使用它们。
But ping/pong
actually uses by socket.io server with several of them.但是
ping/pong
实际上由 socket.io 服务器使用,其中有几个。 you can see the list here .您可以在 此处查看列表。 It's on bottom of the page =)
它在页面底部=)
So unless you respest these you can use upper/lower case event/room names.因此,除非您对这些内容感兴趣,否则您可以使用大写/小写的事件/房间名称。
Also those event's are listenable by user too.这些事件也可由用户收听。
io.on('connect', onConnect);
function onConnect(socket) {
socket.on('error', onError);
socket.on('disconnect', onDisconnect);
// ... and others too.
// You can see and console on ping/pong events too.
socket.on('ping', console.log);
socket.on('pong', console.log);
}
I know socke.io's documentation really is not the best:D我知道 socke.io 的文档确实不是最好的:D
By the way, ping and pong usage is coming from ws
which also used by socket.io internally.顺便说一句,ping 和 pong 的使用来自
ws
,它也被 socket.io 内部使用。 If you want to see more about i'll leave links here where you can see ping
and pong
events emitting.如果您想了解更多关于我将在此处留下链接,您可以在其中看到发出的
ping
和pong
事件。
Sender.js: ping also Receiver.js: ping & pong Sender.js:ping也 Receiver.js: ping & pong
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.