2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Socket.IO
It is a real-time, bidirectional, and event-based communication library based on WebSocket. It was originally designed for Node.js, but now has implementations in multiple languages, including Python. In Python,python-socketio
It is a popular library that allows you to easily implement Socket.IO clients and servers. This library is particularly useful for building web applications that require real-time data exchange, such as chat applications, real-time notification systems, games, etc.
You can install it via pippython-socketio
:
pip install python-socketio
If you also want to create a web server and want it to act as a Socket.IO server at the same time, you may also need to installFlask-SocketIO
(A Flask extension that integrates Flask and Socket.IO):
pip install Flask-SocketIO
The following is a simpleFlask-SocketIO
Server example, which uses the Flask framework and integrates Socket.IO:
- from flask import Flask, render_template
- from flask_socketio import SocketIO, send
-
- app = Flask(__name__)
- app.config['SECRET_KEY'] = 'secret!'
- socketio = SocketIO(app)
-
- @app.route('/')
- def index():
- return render_template('index.html')
-
- @socketio.on('message')
- def handleMessage(msg):
- print('Message: ' + msg)
- send(msg, broadcast=True)
-
- if __name__ == '__main__':
- socketio.run(app)
In this example, the server listens/
Routing, and rendering an HTML page (you need to create this page yourself and import the Socket.IO client library). The server also listens for amessage
When this event is received, it prints the message content and broadcasts the message back to all connected clients.
On the client side (usually JavaScript in a browser), you need to import the Socket.IO client library and establish a connection with the server. Here is a simple client example:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Socket.IO chat</title>
- <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.0/socket.io.js"></script>
- <script>
- var socket = io('http://localhost:5000');
- socket.on('connect', function() {
- console.log('Connected to the server!');
- });
-
- socket.on('message', function(msg) {
- console.log('Received message: ' + msg);
- });
-
- function sendMessage() {
- var input = document.getElementById('messageInput');
- socket.emit('message', input.value);
- input.value = '';
- }
- </script>
- </head>
- <body>
- <input type="text" id="messageInput" autocomplete="off" /><button onclick="sendMessage()">Send</button>
- </body>
- </html>
In this example, the client connects to the server, listeningmessage
event, and defines asendMessage
Function to send a message to the server.
python-socketio
andFlask-SocketIO
Provides powerful tools for building real-time web applications. By leveraging WebSocket and event-driven programming models, you can easily implement complex real-time interactive functions.
We will continue to update and share relevant content in the future.Remember to pay attention!