zhangyuang / ssr

A most advanced ssr framework support React17/React18/Vue2/Vue3 on Earth that implemented serverless-side render specification.
http://doc.ssr-fc.com/
MIT License
2.61k stars 284 forks source link

接入socket问题 #124

Closed wuyiifan closed 3 years ago

wuyiifan commented 3 years ago

详细描述你的问题

按照mindway文档添加socket服务一直报错

{"message":"Converting circular structure to JSON\n    --> starting at object with constructor 'ClientRequest'\n    |     property 'socket' -> object with constructor 'Socket'\n    --- property '_httpMessage' closes the circle","stack":"TypeError: Converting circular structure to JSON\n    --> starting at object with constructor 'ClientRequest'\n    |     property 'socket' -> object with constructor 'Socket'\n    --- property '_httpMessage' closes the circle\n    at JSON.stringify (<anonymous>)\n    at respond (/Users/tiger/Documents/my-links/node_modules/koa/lib/application.js:279:15)\n    at handleResponse (/Users/tiger/Documents/my-links/node_modules/koa/lib/application.js:166:34)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5)","name":"TypeError","status":500}

也添加了启动文件bootstrap.js

const WebFramework = require('@midwayjs/koa').Framework
const SocketFramework = require('@midwayjs/ws').Framework
const { Bootstrap } = require('@midwayjs/bootstrap')

// 加载主 web 框架
const webFramework = new WebFramework().configure({ port: 8000 })

// 加载副 ws 框架,自动适配主框架,这里不需要配置 port
const socketFramework = new SocketFramework().configure({})

Bootstrap
  .load(webFramework)
  .load(socketFramework)
  .run()

src/socket/modal.ts

import { WSController, Provide, OnWSConnection, OnWSMessage, Inject, WSEmit } from '@midwayjs/decorator'
import { Context } from '@midwayjs/socketio'

@Provide()
@WSController()
export class HelloSocketController {
  @Inject()
  ctx: Context

  @OnWSMessage('loginModal')
  @WSEmit('loginModalResult', 'modal')
  async gotMessage () {
    return 'hello world' // 这里将 hello world 字符串返回给客户端
  }

  @OnWSConnection()
  async onConnectionMethod () {
    console.log('on client connect', this.ctx.id)
  }
}

package.json

"scripts": {
     ///
    "start": "ssr start --entryFile=./src/bootstrap.js",
  }

期望的结果

能正常使用socket服务

能在文档上补充一下demo也是很好的

当前使用的版本

不要贴 package.json 用 yarn list "ssr*" 来查看版本。同时建议熟读semver 规则并且知道怎么安装最新依赖!

复现仓库地址

超过一行代码能描述的问题必须提供复现地址, 用默认的 example 给最简单的复现代码不要给包含着一堆业务代码的仓库

你本人对问题可能的原因判断(如果你能大概判断的话)

zhangyuang commented 3 years ago

midway/egg的问题去它们那里找答案。and你这里web框架应该是midway/web