问题描述
我已经在这里写了关于我要创建的http聊天服务器:
此http服务器应该向网站上同一聊天室中的每个用户流式传输文本。浏览器将保持连接并等待进一步的html代码。 (是的,工程,浏览器不会拒绝连接)。
我有一个新问题:因为这个聊天服务器不需要从客户端接收信息,没有必要在服务器发送其第一个响应后监听客户端。新的聊天消息将在新的连接上发送到服务器。
所以我可以打开2个线程,一个等待新的客户端(或新消息)和一个html流。
这是一个好主意,还是应该为每个客户端使用一个线程?我不认为有一个线程/客户端,当有许多在线聊天用户是好的,因为服务器应该处理与自己的房间多个不同的聊天。
3可能性:
1.一个线程为所有客户端,发送文本到每个客户端连续 - 不应该有很多滞后,因为它只有文本
这将是像user1.send(text); user2.send(text),...
2.每个聊天或聊天室一个线程
3.每个聊天用户一个线程... ...许多...
这个简单的应用程序最简单的模式是有线程池,然后为每个客户端选择可用线程或使它等待,直到有一个可用。
如果你想认真理解http服务器架构概念google:
- apache架构
- nginx架构
I already wrote here about the http chat server I want to create: http://stackoverflow.com/questions/2352220/alternative-http-portThis http server should stream text to every user in the same chat room on the website. The browser will stay connected and wait for further html code. (yes that works, the browser won't reject the connection).
I got a new question: Because this chat server doesn't need to receive information from the client, it's not necessary to listen to the client after the server sent its first response. New chat messages will be send to the server on a new connection.So I can open 2 threads, one waiting for new clients (or new messages) and one for the html streaming.Is this a good idea or should I use one thread per client? I don't think it's good to have one thread/client when there are many chat users online, since the server should handle multiple different chats with their own rooms.
3 posibilities:1. One thread for all clients, send text to each client successive - there shouldn't be much lag since it's only textthis will be like: user1.send("text");user2.send("text"),...2. One thread per chat or chatroom3. One thread per chat user - ... many...
Thank you, I haven't done much with sockets yet ;).
I think easiest pattern for this simple app is to have pool of threads and then for each client pick available thread or make it wait until one becomes available.
If you want serious understanding of http server architecture concepts google following:
- apache architecture
- nginx architecture
这篇关于HTTP流服务器:线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!