编写聊天应用程序 [英] Writing a chat application

查看:117
本文介绍了编写聊天应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为成千上万的用户编写聊天应用程序的正确方法是什么。

I'm wondering what's the right way to code a chat application for thousands of users.

我很困惑我怎么能够使用AJAX每秒或更短时间ping服务器并检查MySQL中是否有新记录,服务器负载量是否可接受。

我目前正在考虑使用jQuery,PHP和MySQL进行编码。

I'm currently thinking about coding this using jQuery, PHP and MySQL.

请指教。非常感谢您的帮助。

Please advice. Your help would be greatly appreciated.

推荐答案

客户端



任何需要轮询服务器的程序我都建议 WebSockets

我写了极其基本的WebSocket教程。我还使用了web-socket-js代码来实现一个FlashSocket,它可以在Firefox,IE 8+和Chrome以及支持WebSockets的任何浏览器上运行。

I wrote an extremely basic WebSocket tutorial. I also used the web-socket-js code to implement a FlashSocket that will make it work across Firefox, IE 8+, and Chrome, as well as any browser that supports WebSockets.

我不认为民意调查对于聊天应用程序来说是个不错的选择。虽然它可以工作,但是请求开销会比使用WebSocket高得多。权衡(好处)是更多的浏览器支持它。

I don't believe that polling would be a good choice for a chat application. While it would work, the request overhead would be much higher then using a WebSocket. The tradeoff (benefit) is that more browsers support it.

此外,点击MySQL数据库以查看是否有消息将导致大量的数据库开销。我建议使用MySQL数据库进行聊天记录,并且只保留有限数量的后退消息,以便进行新的连接。然后只需向所有连接的客户端广播新消息。然后,前端应用程序将接收消息并将其附加到聊天窗口。

Also, hitting a MySQL database to see if there are messages is going to incur a good deal of DB overhead. I would recommend using a MySQL database for chat logs, and only keep a limited number of "back" messages on hand for new connections. Then simply broadcast new messages to all connected clients. The frontend application would then take the message and append it to chat window.

Node.js是一个公平的服务器端JavaScript框架。虽然它还很年轻,但已经编写了几个非常有趣的应用程序。 Node.js人员设置聊天程序(而不是WebSockets),其来源为已经可用。如果不想从头开始编写,这将是一个非常好的起点。

Node.js is an evented server-side JavaScript framework. While it is still young, several very interesting applications have been coded in it. The Node.js people setup a chat program (not WebSockets) the source of which has been made available. That would be a very good place to start if not wanting to write it from scratch.

有一个 PHP WebSocket 实现。根据您的要求,它可以使用得很好。在Node.js和PHP中编码我会说我认为Node.js更适合这个。

There is a PHP WebSocket implementation. Depending on your requirements it could be used just fine. Having coded in both Node.js and PHP I would say I think Node.js is a better fit for this.

这篇关于编写聊天应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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