节点JS - 创建持久性私人聊天室 [英] Node js - Creating persistent private chat rooms

查看:119
本文介绍了节点JS - 创建持久性私人聊天室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读了这么多回合的节点JS最近,和聊天功能显得非常漂亮。不过,我见过的唯一聊天的例子基本上广播一个聊天服务器,以一个固定的网址(如会议室)。是否有可能使用节点JS部分创建一个聊天客户端更喜欢谷歌聊天软件? - 其中一个聊天窗口弹起在当前页上,然后通过多个页面仍然存在。有没有人见过这样的例子吗?

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)?

感谢。

推荐答案

我给你一个假的实现依赖于 jQuery的和<一HREF =htt​​p://nowjs.com/>即时抽象掉繁琐的IO和繁琐的DOM操作的解决方案。

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));
};

我才刚刚发现自己说nowjs的新版本(0.5)具有集群系统在建。这基本上是你想要你的东西。没有麻烦。

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.

如果你愿意,你可以删除nowjs的依赖,并与100/200行code代替。我会离开,作为一个练习的用户。

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.

这篇关于节点JS - 创建持久性私人聊天室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆