跳转至

Socket.IO

Socket.IO is a real-time communication framework that enables real-time bidirectional communication between the client and the server. It uses WebSockets as a transport layer and provides a simple API for real-time communication. Socket.IO is a JavaScript library that runs in the browser and enables real-time communication between the client and the server.

Resources

Socket.IO in Python

Socket.IO can be used in Python using the python-socketio library. The library provides a client-side and a server-side implementation. The client-side implementation is used to connect to the server and send and receive messages. The server-side implementation is used to handle incoming connections and send messages to the clients.

Here's an example of how to use Socket.IO in Python:

import socketio

sio = socketio.Client()

@sio.event
def connect():
    print('connection established')


@sio.event
def message(data):
    print('message received with ', data)
    sio.emit('response', {'response': 'my response'})


@sio.event
def disconnect():
    print('disconnected from server')


sio.connect('http://localhost:5000')
  1. First, we import the socketio library.
  2. We create a socketio.Client object.
  3. We define three event handlers: connect, message, and disconnect.
  4. In the connect event handler, we print a message to indicate that the connection has been established.
  5. In the message event handler, we print the received message and send a response using the emit method.
  6. In the disconnect event handler, we print a message to indicate that the connection has been lost.
  7. We connect to the server using the connect method and pass the URL of the server as an argument.

Note that the emit method is used to send a message to the server. The first argument is the event name, and the second argument is the data to be sent. In this example, we send a response to the client with the response event name.

The server-side implementation is a bit more complex, but it can be done using the flask-socketio library. Here's an example of how to use Socket.IO in Python with Flask:

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
app.config['SECRET_KEY'] ='secret!'
socketio = SocketIO(app)

@app.route('/')
def index():
    return render_template('index.html')

@socketio.on('connect')
def connect():
    print('connected')

@socketio.on('message')
def message(data):
    print('message received with ', data)
    emit('response', {'response': 'my response'})

@socketio.on('disconnect')
def disconnect():
    print('disconnected')


if __name__ == '__main__':
    socketio.run(app, debug=True)
  1. First, we import the Flask, render_template, SocketIO, and emit functions from the flask, flask_socketio, and socketio libraries, respectively.
  2. We create a Flask app object and set the SECRET_KEY configuration variable.
  3. We create a SocketIO object and pass the app object as an argument.
  4. We define three event handlers: connect, message, and disconnect.
  5. In the connect event handler, we print a message to indicate that a client has connected.
  6. In the message event handler, we print the received message and send a response using the emit function.
  7. In the disconnect event handler, we print a message to indicate that a client has disconnected.
  8. We run the app using the run method and pass the app object as an argument.

In this example, we use the emit function to send a message to the client with the response event name. The first argument is the event name, and the second argument is the data to be sent. In this example, we send a response to the client with the response event name.

Note that the emit function is used to send a message to the client. The first argument is the event name, and the second argument is the data to be sent. In this example, we send a response to the client with the response event name.

The client-side implementation is a bit more complex, but it can be done using the socket.io-client library. Here's an example of how to use Socket.IO in Python with a JavaScript client:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Socket.IO Example</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
</head>
<body>
  <h1>Socket.IO Example</h1>
  <p id="message"></p>
  <script>
    const socket = io();

    socket.on('connect', () => {
      console.log('connected');
      socket.emit('message', 'Hello, server!');
    });

    socket.on('response', (data) => {
      console.log('response received with ', data);
      document.getElementById('message').innerHTML = data.response;
    });

    socket.on('disconnect', () => {
      console.log('disconnected');
    });
  </script>
</body>
</html>
  1. First, we import the socket.io-client library.
  2. We create a socket object and pass the URL of the server as an argument.
  3. We define three event handlers: connect, response, and disconnect.
  4. In the connect event handler, we print a message to indicate that the connection has been established.
  5. In the response event handler, we print the received message and update the message element with the response.
  6. In the disconnect event handler, we print a message to indicate that the connection has been lost.

Note that the emit method is used to send a message to the server. The first argument is the event name, and the second argument is the data to be sent. In this example, we send a message to the server with the message event name.

The server-side implementation is a bit more complex, but it can be done using the socket.io library. Here's an example of how to use Socket.IO in Python with a JavaScript server:

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require('socket.io');

const io = new Server(server, {
  cors: {
    origin: '*',
  },
});

io.on('connection', (socket) => {
  console.log('a user connected');

  socket.on('message', (data) => {
    console.log('message received with ', data);
    socket.emit('response', { response: 'my response' });
  });

  socket.on('disconnect', () => {
    console.log('user disconnected');
  });
});

server.listen(3000, () => {
  console.log('listening on *:3000');
});
  1. First, we import the express, http, and socket.io libraries.
  2. We create an app object and a server object using the http library.
  3. We create a socket.io object and pass the server object as an argument.
  4. We define an event handler for the connection event.
  5. In the connection event handler, we print a message to indicate that a client has connected.
  6. We define an event handler for the message event.
  7. In the message event handler, we print the received message and send a response using the emit method.
  8. We define an event handler for the disconnect event.
  9. In the disconnect event handler, we print a message to indicate that a client has disconnected.
  10. We start the server using the listen method and pass the port number as an argument.
  11. We print a message to indicate that the server is listening on the specified port.

In this example, we use the emit method to send a message to the client with the response event name. The first argument is the event name, and the second argument is the data to be sent. In this example, we send a response to the client with the response event name.

Note that the cors option is used to allow cross-origin requests. This is necessary for Socket.IO to work between the client and the server.

Overall, Socket.IO is a powerful tool for real-time communication between the client and the server. It provides a simple API for real-time communication and is easy to use in Python and JavaScript.