Node js - Creating persistent private chat rooms

橙三吉。 提交于 2019-11-30 05:24:20

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5621642/node-js-creating-persistent-private-chat-rooms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!