我想实现一个简单的通知系统。当user1喜欢的user2帖子时,user2应该会收到来自的实时通知user1。
这是客户端功能(Redux 操作)的一部分,其中有人喜欢帖子:
.then(() => {
const socket = require("socket.io-client")(
"http://localhost:5000"
);
socket.emit("like", user, post);
});
Run Code Online (Sandbox Code Playgroud)
user1这是服务器套接字函数,在Likesuser2的帖子之后创建通知:
io.on("connection", (socket) => {
socket.on("like", async (user, post) => {
if (!post.post.likedBy.includes(user.user._id)) {
const Notification = require("./models/Notification");
let newNotification = new Notification({
notification: `${user.user.username} liked your post!`,
sender: user.user._id,
recipient: post.post.by._id,
read: false,
});
await newNotification.save();
io.emit("notification");
}
});
});
Run Code Online (Sandbox Code Playgroud)
这是创建通知后的客户端函数:
socket.on("notification", () => {
console.log("liked");
});
Run Code Online (Sandbox Code Playgroud)
现在的问题是和console.log('liked')都出现。我怎样才能只发送给收到通知的用户?socket.io …
http://localhost:3000/?search=test&type=abc&category=xyz
当我搜索 test(以及类型和类别)后,URL 更改为上面的 URL。
return router.push(
{
pathname: `/`,
query: {
// something to do here...
},
},
"",
{ scroll: false }
);
Run Code Online (Sandbox Code Playgroud)
接下来,我有一个onClick与此有关的router.push.
我想仅删除搜索查询并保留类型和类别查询。这怎么可能?该文档没有提及任何相关内容。
我里面有一个navbar和一个联系按钮。我想根据 URL将此“联系”按钮更改为“后退”按钮。
如果 url 是localhost:5000/那么就让它成为Contact,如果 url 那么localhost:5000/about它应该是Back。
这可能吗?我不确定哪种语法可以做到这一点。
{localhost:5000/ && <button>Contact</button>}
{localhost:5000/about && <button>Back</button>}
Run Code Online (Sandbox Code Playgroud)
这显然是不正确的,我只是想知道是否有类似的方法。