如何建立一个可以在服务器运行期间创建新频道的聊天室?

发布于 2024-12-22 13:27:25 字数 293 浏览 4 评论 0原文

阅读了很长一段时间有关 node.js 的文章后,我尝试使用 socket.io 编写自己风格的聊天室。 这是链接,但只有一个频道:http://zhonglichat.cnodejs.net/ 这是我的想法: 访问者可以通过输入标题并单击“提交”来创建新频道。 然后他们也可以像访问另一个聊天室一样访问新频道。 而这一切都是在服务器运行时发生的。我浏览了 socket.io 上的指南,似乎很难创建新通道。那么有什么简单的方法可以让我做到这一点吗?

Having read posts about node.js for a long time, I tried to write my own style of chatroom with socket.io .
Here's the link, but only one channel: http://zhonglichat.cnodejs.net/
Here's what I'm thinking about:
Visitors can create new channels by typing title and clicking 'Submit'.
Then they can also visit the new channel just like visiting another chatroom.
And all these happen while the server is running. I went through the guide on socket.io , seems it's hard to create new channels. So is there any simple way via which I can make that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2024-12-29 13:27:25

不需要使用最新版本的 Socket.IO 创建通道,重要的是让客户端加入它,在服务器端查看此代码示例(来自 github 存储库):

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.join('justin bieber fans');
  socket.broadcast.to('justin bieber fans').emit('new fan');
  io.sockets.in('rammstein fans').emit('new non-fan');
});

如您所见,“Justin bieber 粉丝”频道尚未创建,但客户可以加入它。

There's no need to create a channel with the latest version of Socket.IO, the important thing is just to make clients join it, look at this code example on the server side (from the github repo):

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.join('justin bieber fans');
  socket.broadcast.to('justin bieber fans').emit('new fan');
  io.sockets.in('rammstein fans').emit('new non-fan');
});

As you can see the 'justin bieber fans' channel wasn't created, but clients can join it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文