You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/12/06 06:21:40 UTC

[GitHub] [dubbo-js] godkun commented on issue #322: Mvp 版本讨论

godkun commented on issue #322:
URL: https://github.com/apache/dubbo-js/issues/322#issuecomment-1338828137

   调整后的 transport mvp
   
   pr入口:https://github.com/apache/dubbo-js/pull/325
   
   入口
   
   ```ts
   import { DubboClientTransport } from './client'
   import { DubboServerTransport } from './server'
   
   export { DubboClientTransport, DubboServerTransport }
   ```
   
   client transport 代码
   
   ```ts
   import { debug } from 'debug'
   import http2 from 'node:http2'
   import { IDubboClientTransport, DubboContext } from './transport'
   
   // init log
   const log = debug('dubbo3:transport:client')
   
   export class DubboClientTransport implements IDubboClientTransport {
     // transport 实例
     private transport: any
     private ctx: DubboContext
   
     constructor(opts: DubboContext) {
       this.ctx = opts
       this.connect()
     }
   
     get url() {
       return this.ctx.url
     }
   
     /**
      * 建立连接
      */
     connect() {
       this.transport = http2.connect(this.url)
   
       this.transport.once('connect', () => {
         log('has connected')
       })
     }
   
     /**
      * 发送消息
      * @param msg
      */
     async send(msg: DubboContext): Promise<void> {
       this.transport.request(msg)
     }
   }
   ```
   
   server transport 代码:
   
   ```ts
   import debug from 'debug'
   import EventEmitter from 'node:events'
   import http2 from 'node:http2'
   import { IDubboServerTransport, DubboContext } from './transport'
   
   // init log
   const log = debug('dubbo3:transport:client')
   
   export class DubboServerTransport
     extends EventEmitter
     implements IDubboServerTransport
   {
     private ctx: DubboContext
     transport: any
   
     constructor(opts: DubboContext) {
       super()
       this.ctx = opts
       this.transport = this.start()
     }
   
     get url() {
       return this.ctx.url
     }
   
     get port() {
       return this.ctx.port
     }
   
     /**
      * 启动服务端 transport
      * @returns
      */
     start() {
       const server = http2.createServer()
       server.on('stream', (stream, headers) => {
         log(stream)
         stream.on('data', (data) => {
           log(data)
           // TODO:
         })
         stream.on('end', () => {
           log('end...')
           // TODO: 通知 client
         })
         stream.on('error', (error) => {
           log(error)
         })
       })
       server.listen(this.port)
       return server
     }
   }
   ```
   
   tansport.ts 接口定义:
   
   ```ts
   export interface DubboContext {
     url: string
     body?: Object | null
     port: number
   }
   
   export interface IDubboClientTransport {
     send(msg: DubboContext): Promise<any>
   }
   
   export interface IDubboServerTransport {
     // start(msg: DubboContext): Promise<any>
   }
   ```
   
   依赖:
   
   - dubbo context 数据和定义
   
   @fengwei5280 
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org