func (p *Peer) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
	replyError := func(err error) {
		_ = conn.ReplyWithError(ctx, req.ID, &jsonrpc2.Error{
			Code:    500,
			Message: fmt.Sprintf("%s", err),
		})
	}

	if req.Params == nil {
		log.Errorf("request without params: uid=%s", p.UID())
		replyError(errors.New("request without params"))
		return
	}

	switch req.Method {
	case proto.ClientJoin:
		var msg proto.FromClientJoinMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing offer: %v", err)
			replyError(err)
			break
		}
		answer, err := p.Join(&msg)
		if err != nil {
			replyError(err)
			break
		}
		_ = conn.Reply(ctx, req.ID, answer)

	case proto.ClientOffer:
		var msg proto.ClientOfferMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing trickle: %v", err)
			replyError(err)
		}
		answer, err := p.Offer(&msg)
		if err != nil {
			replyError(err)
			break
		}
		_ = conn.Reply(ctx, req.ID, answer)

	case proto.ClientAnswer:
		var msg proto.ClientAnswerMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing trickle: %v", err)
			replyError(err)
			break
		}
		if err := p.Answer(&msg); err != nil {
			replyError(err)
		}

	case proto.ClientTrickle:
		var msg proto.ClientTrickleMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing trickle: %v", err)
			replyError(err)
			break
		}
		if err := p.Trickle(&msg); err != nil {
			replyError(err)
		}

	case proto.ClientLeave:
		var msg proto.FromClientLeaveMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing leave: %v", err)
			replyError(err)
			break
		}
		if err := p.Leave(&msg); err != nil {
			replyError(err)
		}

	case proto.ClientBroadcast:
		var msg proto.FromClientBroadcastMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing trickle: %v", err)
			replyError(err)
			break
		}
		if err := p.Broadcast(&msg); err != nil {
			replyError(err)
		}

	default:
		replyError(errors.New("unknown message"))
	}
}
// Handle incoming RPC call events, implement jsonrpc2.Handler
Handle incoming RPC call events, implement jsonrpc2.Handler
// Handle incoming RPC call events implement jsonrpc2 . Handler
func (p *Peer) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
	replyError := func(err error) {
		_ = conn.ReplyWithError(ctx, req.ID, &jsonrpc2.Error{
			Code:    500,
			Message: fmt.Sprintf("%s", err),
		})
	}
	if req.Params == nil {
		log.Errorf("request without params: uid=%s", p.UID())
		replyError(errors.New("request without params"))
		return
	}
	switch req.Method {
	case proto.ClientJoin:
		var msg proto.FromClientJoinMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing offer: %v", err)
			replyError(err)
			break
		}
		answer, err := p.Join(&msg)
		if err != nil {
			replyError(err)
			break
		}
		_ = conn.Reply(ctx, req.ID, answer)
	case proto.ClientOffer:
		var msg proto.ClientOfferMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing trickle: %v", err)
			replyError(err)
		}
		answer, err := p.Offer(&msg)
		if err != nil {
			replyError(err)
			break
		}
		_ = conn.Reply(ctx, req.ID, answer)
	case proto.ClientAnswer:
		var msg proto.ClientAnswerMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing trickle: %v", err)
			replyError(err)
			break
		}
		if err := p.Answer(&msg); err != nil {
			replyError(err)
		}
	case proto.ClientTrickle:
		var msg proto.ClientTrickleMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing trickle: %v", err)
			replyError(err)
			break
		}
		if err := p.Trickle(&msg); err != nil {
			replyError(err)
		}
	case proto.ClientLeave:
		var msg proto.FromClientLeaveMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing leave: %v", err)
			replyError(err)
			break
		}
		if err := p.Leave(&msg); err != nil {
			replyError(err)
		}
	case proto.ClientBroadcast:
		var msg proto.FromClientBroadcastMsg
		if err := p.unmarshal(*req.Params, &msg); err != nil {
			log.Errorf("error parsing trickle: %v", err)
			replyError(err)
			break
		}
		if err := p.Broadcast(&msg); err != nil {
			replyError(err)
		}
	default:
		replyError(errors.New("unknown message"))
	}
}
func ( p * Peer ) Handle ( ctx context . Context , conn * jsonrpc2 . Conn , req * jsonrpc2 . Request ) { replyError := func ( err error ) { _ = conn . ReplyWithError ( ctx , req . ID , & jsonrpc2 . Error { Code : 500 , Message : fmt . Sprintf ( " " , err ) , } ) } if req . Params == nil { log . Errorf ( " " , p . UID ( ) ) replyError ( errors . New ( " " ) ) return } switch req . Method { case proto . ClientJoin : var msg proto . FromClientJoinMsg if err := p . unmarshal ( * req . Params , & msg ) ; err != nil { log . Errorf ( " " , err ) replyError ( err ) break } answer , err := p . Join ( & msg ) if err != nil { replyError ( err ) break } _ = conn . Reply ( ctx , req . ID , answer ) case proto . ClientOffer : var msg proto . ClientOfferMsg if err := p . unmarshal ( * req . Params , & msg ) ; err != nil { log . Errorf ( " " , err ) replyError ( err ) } answer , err := p . Offer ( & msg ) if err != nil { replyError ( err ) break } _ = conn . Reply ( ctx , req . ID , answer ) case proto . ClientAnswer : var msg proto . ClientAnswerMsg if err := p . unmarshal ( * req . Params , & msg ) ; err != nil { log . Errorf ( " " , err ) replyError ( err ) break } if err := p . Answer ( & msg ) ; err != nil { replyError ( err ) } case proto . ClientTrickle : var msg proto . ClientTrickleMsg if err := p . unmarshal ( * req . Params , & msg ) ; err != nil { log . Errorf ( " " , err ) replyError ( err ) break } if err := p . Trickle ( & msg ) ; err != nil { replyError ( err ) } case proto . ClientLeave : var msg proto . FromClientLeaveMsg if err := p . unmarshal ( * req . Params , & msg ) ; err != nil { log . Errorf ( " " , err ) replyError ( err ) break } if err := p . Leave ( & msg ) ; err != nil { replyError ( err ) } case proto . ClientBroadcast : var msg proto . FromClientBroadcastMsg if err := p . unmarshal ( * req . Params , & msg ) ; err != nil { log . Errorf ( " " , err ) replyError ( err ) break } if err := p . Broadcast ( & msg ) ; err != nil { replyError ( err ) } default : replyError ( errors . New ( " " ) ) } }
Handle incoming RPC call events, implement jsonrpc2.Handler
Handle incoming RPC call events implement jsonrpc2 . Handler