简体   繁体   English

使用Python和websocket创建实时聊天

[英]Creating a real-time chat with Python and websocket

I'm writing a python real-time chat feature embedded in a web app. 我正在编写一个嵌入在Web应用程序中的python实时聊天功能。 I'm a little bit confused on the real time implementation. 我对实时实现有点困惑。 I need to push real time message to different users. 我需要将实时消息推送给不同的用户。

I plan to use websocket but I'm not quite sure how to save those sockets into an array so that once a user send a message to server, the server can find the related socket and push the message. 我打算使用websocket,但我不太确定如何将这些套接字保存到数组中,这样一旦用户向服务器发送消息,服务器就可以找到相关的套接字并推送消息。

So any idea about this? 所以对此有任何想法吗? Or what's the common way to implement real time chat feature? 或者实现实时聊天功能的常用方法是什么?

Thanks in advance. 提前致谢。

You need to use a websocket aware web server, like Tornado to handle websocket traffic. 您需要使用支持websocket的Web服务器(如Tornado)来处理websocket流量。 To multiplex chat messages between different chats and users, there are solutions like Redis and ZeroMQ that you can use for message multiplexing. 要在不同聊天和用户之间复用聊天消息,可以使用Redis和ZeroMQ等解决方案进行消息多路复用。

However, it sounds like you have zero experience and starting point, so starting with an working example is better approach. 然而,听起来你没有经验和起点,所以从一个工作的例子开始是更好的方法。 Please study existing real-time chat implementations for Python: 请研究Python的现有实时聊天实现:

https://github.com/heroku-examples/python-websockets-chat https://github.com/heroku-examples/python-websockets-chat

https://github.com/nellessen/Tornado-Redis-Chat https://github.com/nellessen/Tornado-Redis-Chat

https://github.com/tornadoweb/tornado/blob/master/demos/websocket/chatdemo.py https://github.com/tornadoweb/tornado/blob/master/demos/websocket/chatdemo.py

http://ferretfarmer.net/2013/09/05/tutorial-real-time-chat-with-django-twisted-and-websockets-part-1/ http://ferretfarmer.net/2013/09/05/tutorial-real-time-chat-with-django-twisted-and-websockets-part-1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM