Node js - 创建持久的私人聊天室

发布于 2024-10-31 11:00:46 字数 217 浏览 6 评论 0原文

最近读了很多关于 Node js 的文章,聊天功能看起来非常不错。然而,我见过的唯一的聊天示例基本上都是将聊天服务器广播到固定的 URL(如会议室)。是否可以部分使用node js来创建一个更像gchat的聊天客户端? - 在当前页面上弹出聊天窗口,然后在多个页面中持续显示。有人见过这样的例子吗?

如果没有,是否建议使用其他技术来实现此目的(我知道这已在其他问题中得到了回答)?

谢谢。

I've been reading so much a bout node js lately, and the chat capabilities seem very nice. However, the only chat examples I've seen basically broadcast a chat server to a fixed URL (like a meeting room). Is it possible to use node js in part to create a chat client more like gchat? - where a chat window is popped up on the current page and then persists through multiple pages. Has anyone seen an example of this yet?

If not, suggestions for other technologies to use for this purpose (I know that's been answered in other questions)?

Thanks.

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

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

发布评论

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

评论(2

江南烟雨〆相思醉 2024-11-07 11:00:46

我将为您提供一个依赖于 jquery现在从解决方案中抽象出繁琐的 IO 和 DOM 操作。

// Server

var nowjs = require('now');
var everyone = nowjs.initialize(httpServer);

everyone.now.joinRoom = function(room) {
    nowjs.getGroup(room).addUser(this.user.clientId);
}

everyone.now.leaveRoom = function(room) {
    nowjs.getGroup(room).removeUser(this.user.clientId);
}

everyone.now.messageRoom = function(room, message) {
    nowjs.getGroup(room).now.message(message);
}

// Client

var currRoom = "";

$(".join").click(function() {
    currRoom = ...
    now.joinRoom(currRoom);
});

$(".send").click(function() {
    var input = ...
    now.messageRoom(currRoom, input.text());
});

now.messageRoom = function(message) {
    $("messages").append($("<div></div>").text(message));
};

我刚刚注意到,新版本的 nowjs (0.5) 已经构建了群组系统。这基本上可以满足您的需求。没有麻烦。

如果您愿意,可以删除 nowjs 依赖项并将其替换为 100/200 行代码。我将把它作为练习留给用户。

I'll give you a pseudo implementation relying on jquery and now to abstract away tedious IO and tedious DOM manipulation from the solution.

// Server

var nowjs = require('now');
var everyone = nowjs.initialize(httpServer);

everyone.now.joinRoom = function(room) {
    nowjs.getGroup(room).addUser(this.user.clientId);
}

everyone.now.leaveRoom = function(room) {
    nowjs.getGroup(room).removeUser(this.user.clientId);
}

everyone.now.messageRoom = function(room, message) {
    nowjs.getGroup(room).now.message(message);
}

// Client

var currRoom = "";

$(".join").click(function() {
    currRoom = ...
    now.joinRoom(currRoom);
});

$(".send").click(function() {
    var input = ...
    now.messageRoom(currRoom, input.text());
});

now.messageRoom = function(message) {
    $("messages").append($("<div></div>").text(message));
};

I only just noticed myself that the new version of nowjs (0.5) has the group system in build. This basically does what you want for you. No hassle.

If you want you can remove the nowjs dependency and replace it with 100/200 lines of code. I'll leave that as an exercise for the user.

风追烟花雨 2024-11-07 11:00:46

看看 AjaxIM: https://github.com/freq32/AjaxIM

这是 facebook 风格基于nodejs的聊天应用程序(想想朋友列表、屏幕底部的小型持久聊天栏、弹出聊天)。

Take a look at AjaxIM: https://github.com/freq32/AjaxIM

This is a facebook-style chat application (think friends list, small persistent chat bar at the bottom of the screen, popup chats) based on nodejs.

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