跳到主要内容

SocketIO 怎么用?

前提条件:

  1. 这个sockeio要在生产环境中, 意思是启动并不是bench start 启动的。

  2. 查看如下的配置, 看是否有redis_socketio 和 socketio_port

    frappe@e8811978c502:~/frappe-bench/sites$ cat common_site_config.json 
    {
    "background_workers": 1,
    "developer_mode": 1,
    "dns_multitenant": false,
    "file_watcher_port": 6787,
    "frappe_user": "frappe",
    "gunicorn_workers": 5,
    "http_timeout": 6000,
    "live_reload": true,
    "maintenance_mode": 0,
    "no-cors": 1,
    "pause_scheduler": 0,
    "rebase_on_pull": false,
    "redis_cache": "redis://localhost:13000",
    "redis_queue": "redis://localhost:11000",
    "redis_socketio": "redis://localhost:12000",
    "restart_supervisor_on_update": true,
    "restart_systemd_on_update": false,
    "serve_default_site": true,
    "shallow_clone": true,
    "socketio_port": 9000,
    "use_redis_auth": false,
    "webserver_port": 8000
    }
  3. 查看进程是否有在运行。

    frappe@e8811978c502:~/frappe-bench/sites/site1.local$ sudo supervisorctl status all
    frappe-bench-node-socketio RUNNING pid 57942, uptime 10:42:40
    frappe-bench-redis:frappe-bench-redis-cache RUNNING pid 57943, uptime 10:42:40
    frappe-bench-redis:frappe-bench-redis-queue RUNNING pid 57944, uptime 10:42:40
    frappe-bench-redis:frappe-bench-redis-socketio RUNNING pid 57945, uptime 10:42:40
    frappe-bench-web:frappe-bench-frappe-web RUNNING pid 57946, uptime 10:42:40
    frappe-bench-workers:frappe-bench-frappe-default-worker-0 RUNNING pid 57953, uptime 10:42:40
    frappe-bench-workers:frappe-bench-frappe-long-worker-0 RUNNING pid 57968, uptime 10:42:40
    frappe-bench-workers:frappe-bench-frappe-schedule RUNNING pid 57952, uptime 10:42:40
    frappe-bench-workers:frappe-bench-frappe-short-worker-0 RUNNING pid 57962, uptime 10:42:40
    mariadb RUNNING pid 57941, uptime 10:42:40
    nginx RUNNING pid 57969, uptime 10:42:40
    sshd FATAL Exited too quickly (process log may have details)
  4. 查看一下浏览器这里的console是否有报错。

  5. 最后通过如下的方法来确认socketio工作没有问题。

    C:\Users\jianc>curl "http://192.168.32.2/socket.io/?EIO=4&transport=polling"
    0{"sid":"e2xXQht7jqCwrRihAACP","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":20000,"maxPayload":1000000}

用法:

后续的调试, 客户端直接用浏览器的控制台就可以, 服务端用服务器的bench console

bench console的进入方式:

frappe@e8811978c502:~/frappe-bench$ bench use site1.local
Current Site set to site1.local
frappe@e8811978c502:~/frappe-bench$ bench console
Apps in this namespace:
frappe, erpnext, erpnextcn, meeting

In [1]: frappe.publish_realtime("custom_event")

方法1: 确认连接可工作

client:
frappe.realtime.on("custom_event",() => { console.log("Hello,call from back.")});

server:
frappe.publish_realtime("custom_event")

服务端发送完之后, 可以在浏览器的控制台得到输出。