egg icon indicating copy to clipboard operation
egg copied to clipboard

关于EGG-SOCKET.IO 线上部署后的问题

Open z253573760 opened this issue 8 years ago • 16 comments

通过接口访问服务器 触发socket.io 像其他客户端 发送信息 在win下 整套流程是可以的 。 部署到线上后, 就失效了, 客户端接受不到 信息,但是 客户端和服务器 的socket 心跳 都是正常的 一些由客户端主动给服务端的 socket 信息 也是正常的

线上部署的 npm start 指令 "start": "egg-scripts start --sticky --title=egg-server",

想问下 这个大概是什么原因 是代码问题 还是部署问题 从哪里排查

z253573760 avatar May 16 '18 02:05 z253573760

Translation of this issue:


Questions about EGG-SOCKET.IO Online Deployment

Accessing the server through the interface Triggering socket.io Sending information like other clients The whole process is possible under win. After deploying to the line, it will be invalid, the client will not receive the message, but the heartbeat of both the client and the server will be normal. Some socket information that is actively provided by the client to the server is also normal

The online deployment of the npm start directive "start": "egg-scripts start --sticky --title=egg-server",

I would like to ask if this is probably due to a code issue or a deployment issue.

egg-bot avatar May 16 '18 02:05 egg-bot

简单说 就是在线上 除了在io下的contoroller 的 socket 能发送 在其他里面都发送不了
在service 下 和 contorller 下socket 的发送不了

z253573760 avatar May 16 '18 03:05 z253573760

或者 说 在 ctx.service中 不能调用 app.io.emit 或者 .io.to(socket_id).emit 的方法吗?

z253573760 avatar May 16 '18 04:05 z253573760

比如说 这是在 image ;

image 本地 WIN 下 可以 在线上部署后就没反应 这是什么情况

z253573760 avatar May 16 '18 04:05 z253573760

嘿,哥们,解决了?如何解决的

DragWeb avatar Jun 28 '18 11:06 DragWeb

@z253573760 你的问题和我的一样,请问你是怎么解决的?

Faynman-Dragon avatar Jun 28 '18 11:06 Faynman-Dragon

貌似找到原因了,因为本地开发部署时(npm run dev)只会开启一个进程,此时socket和http都会在同一个进程下进行,所以客户端可以正常收发消息;而在服务器上部署(npm start),这种方式会创建多个进程,当socket和http不在同一个进程下就会出现客户端收不到消息的情况,这个是http和socket的原理有关,我也是不大懂。解决办法是在start的script添加--worket=1就好了,意思是设置启动为1个线程。

Faynman-Dragon avatar Jun 29 '18 02:06 Faynman-Dragon

The production deoply issue has been clearly documented at here http://eggjs.org/en/tutorials/socketio.html#deployment please check it first.

ngot avatar Jun 29 '18 04:06 ngot

@ngot Add --sticky at start script?I've been adding this configuration all the time.But it doesn't work.

Faynman-Dragon avatar Jun 29 '18 06:06 Faynman-Dragon

I created three client.Only one takes effect.

Faynman-Dragon avatar Jun 29 '18 06:06 Faynman-Dragon

Maybe, you did not make it the right way. It is better to provide a reproducible repo and link it here, so we can figure out what is going wrong.

ngot avatar Jun 29 '18 06:06 ngot

//server /package.json
"scripts": {
    "start": "egg-scripts start --title=egg-server-qez_client_sync --sticky --workers=1",
    "start2": "egg-scripts start --daemon --title=egg-server-qez_client_sync --sticky",
    "stop": "egg-scripts stop --title=egg-server-qez_sync",
    "dev": "egg-bin dev --sticky --port 7002",
    "debug": "egg-bin debug",
    "test": "npm run lint -- --fix && npm run test-local",
    "test-local": "egg-bin test",
    "cov": "egg-bin cov",
    "lint": "eslint .",
    "ci": "npm run lint && npm run cov",
    "autod": "autod"
  },
//server /app/controller/msg.js
async testMsg(){
    let msg = this.ctx.query.msg;
    console.log(msg)
    var nsp = this.ctx.app.io.of('/sync');
    nsp.emit('testMsg', msg);
    this.ctx.body = 'hi, '+msg;
  }
//client /main.js
socket = io(`${serverUrl}/sync`, {
        extraHeaders: { client_version: VERSION }
      });
socket.on('testMsg', function (msg) {
        log("收到消息:"+msg)
      });

Faynman-Dragon avatar Jun 29 '18 07:06 Faynman-Dragon

我创建了三个客户端。只有一个生效。

同样的问题 怎么解决的??

shaojun427 avatar Apr 30 '19 03:04 shaojun427

没有解决方法了吗?

leosj avatar Jul 25 '19 08:07 leosj

兼顾一下保证客户端全部连到同一个进程里,如 agent 进程

// agent.ts
import { Agent } from 'egg';
import * as socketIO from 'socket.io';
const io = socketIO(3000, {
});
const nsp = io.of('/api/v1/ws');

export default function (agent: Agent) {
  agent.messenger.on('desk-order-update', (data) => {
    nsp.emit(`desk-order-update-${data.deskId}`, data);
  });
}

最好的方法还是 socket.io 单独一个服务

wivwiv avatar Sep 25 '19 18:09 wivwiv

兼顾一下保证客户端全部连到同一个进程里,如 agent 进程

// agent.ts
import { Agent } from 'egg';
import * as socketIO from 'socket.io';
const io = socketIO(3000, {
});
const nsp = io.of('/api/v1/ws');

export default function (agent: Agent) {
  agent.messenger.on('desk-order-update', (data) => {
    nsp.emit(`desk-order-update-${data.deskId}`, data);
  });
}

最好的方法还是 socket.io 单独一个服务

wivwiv avatar Sep 25 '19 18:09 wivwiv