peer 코드 정리
This commit is contained in:
@ -4,13 +4,9 @@ package wshandler
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"repositories.action2quare.com/ayo/gocommon/session"
|
||||
)
|
||||
|
||||
type TestReceiver struct {
|
||||
@ -60,57 +56,6 @@ func TestUnmarshalToken(t *testing.T) {
|
||||
// con.AddHandler(receiver)
|
||||
|
||||
}
|
||||
|
||||
type testpeer struct {
|
||||
id string
|
||||
}
|
||||
|
||||
func (ph *testpeer) ApiFunc1(arg1 string, arg2 int) error {
|
||||
fmt.Println("ApiFunc1", ph.id, arg1, arg2)
|
||||
return errors.New("fake")
|
||||
}
|
||||
|
||||
func (ph *testpeer) ApiFunc2(arg1 string, arg2 map[string]int) (string, error) {
|
||||
fmt.Println("ApiFunc2", ph.id, arg1, arg2)
|
||||
return "success", nil
|
||||
}
|
||||
|
||||
func (ph *testpeer) ApiFunc3(arg1 float64, arg2 []int) {
|
||||
fmt.Println("ApiFunc3", ph.id, arg1, arg2)
|
||||
}
|
||||
|
||||
func (ph *testpeer) ClientDisconnected(reason string) {
|
||||
}
|
||||
|
||||
func (ph *testpeer) ClientConnected(*websocket.Conn) {
|
||||
}
|
||||
|
||||
type dummySessionConsumer struct {
|
||||
}
|
||||
|
||||
func (dsc *dummySessionConsumer) Query(string) (session.Authorization, error) {
|
||||
return session.Authorization{}, nil
|
||||
}
|
||||
func (dsc *dummySessionConsumer) Touch(string) (session.Authorization, error) {
|
||||
return session.Authorization{}, nil
|
||||
}
|
||||
|
||||
func TestPeerApiBroker(t *testing.T) {
|
||||
ws := NewWebsocketPeerHandler[*testpeer](&dummySessionConsumer{}, "test")
|
||||
|
||||
tp := &testpeer{
|
||||
id: "onlyone",
|
||||
}
|
||||
func1args, _ := json.Marshal([]any{string("arg1"), int(100)})
|
||||
ws.Call(tp, "test.ApiFunc1", bytes.NewBuffer(func1args))
|
||||
|
||||
func1args, _ = json.Marshal([]any{string("arg1"), map[string]int{"arg2.key": 99}})
|
||||
ws.Call(tp, "test.ApiFunc2", bytes.NewBuffer(func1args))
|
||||
|
||||
func1args, _ = json.Marshal([]any{float64(111.1), []int{99, 98}})
|
||||
ws.Call(tp, "test.ApiFunc3", bytes.NewBuffer(func1args))
|
||||
}
|
||||
|
||||
func TestUnmarshal(t *testing.T) {
|
||||
src := []byte(`{"123" :"str1", "456": "str2"}`)
|
||||
var test map[int]string
|
||||
|
||||
@ -19,7 +19,11 @@ import (
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type WebsocketPeerHandler[T PeerInterface] struct {
|
||||
type WebsocketPeerHandler interface {
|
||||
RegisterHandlers(serveMux *http.ServeMux, prefix string) error
|
||||
}
|
||||
|
||||
type websocketPeerHandler[T PeerInterface] struct {
|
||||
methods map[string]peerApiFuncType[T]
|
||||
CreatePeer func(primitive.ObjectID) T
|
||||
sessionConsumer session.Consumer
|
||||
@ -36,7 +40,7 @@ type websocketPeerApiHandler[T PeerInterface] struct {
|
||||
originalReceiverName string
|
||||
}
|
||||
|
||||
func (hc *WebsocketPeerHandler[T]) Call(recv T, funcname string, r io.Reader) (v any, e error) {
|
||||
func (hc *websocketPeerHandler[T]) call(recv T, funcname string, r io.Reader) (v any, e error) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
if r != nil {
|
||||
@ -131,7 +135,7 @@ func makeWebsocketPeerApiHandler[T PeerInterface](receiverName string) websocket
|
||||
}
|
||||
}
|
||||
|
||||
func NewWebsocketPeerHandler[T PeerInterface](consumer session.Consumer, receiverName string) WebsocketPeerHandler[T] {
|
||||
func NewWebsocketPeerHandler[T PeerInterface](consumer session.Consumer, receiverName string) WebsocketPeerHandler {
|
||||
methods := make(map[string]peerApiFuncType[T])
|
||||
receiver := makeWebsocketPeerApiHandler[T](receiverName)
|
||||
|
||||
@ -141,13 +145,13 @@ func NewWebsocketPeerHandler[T PeerInterface](consumer session.Consumer, receive
|
||||
methods[k] = v
|
||||
}
|
||||
|
||||
return WebsocketPeerHandler[T]{
|
||||
return &websocketPeerHandler[T]{
|
||||
sessionConsumer: consumer,
|
||||
methods: methods,
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *WebsocketPeerHandler[T]) RegisterHandlers(serveMux *http.ServeMux, prefix string) error {
|
||||
func (ws *websocketPeerHandler[T]) RegisterHandlers(serveMux *http.ServeMux, prefix string) error {
|
||||
url := gocommon.MakeHttpHandlerPattern(prefix, "ws")
|
||||
if *noAuthFlag {
|
||||
serveMux.HandleFunc(url, ws.upgrade_nosession)
|
||||
@ -158,7 +162,7 @@ func (ws *WebsocketPeerHandler[T]) RegisterHandlers(serveMux *http.ServeMux, pre
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ws *WebsocketPeerHandler[T]) upgrade_core(conn *websocket.Conn, accid primitive.ObjectID, nonce uint32) {
|
||||
func (ws *websocketPeerHandler[T]) upgrade_core(conn *websocket.Conn, accid primitive.ObjectID, nonce uint32) {
|
||||
go func(c *websocket.Conn, accid primitive.ObjectID) {
|
||||
peer := ws.CreatePeer(accid)
|
||||
var closeReason string
|
||||
@ -200,7 +204,7 @@ func (ws *WebsocketPeerHandler[T]) upgrade_core(conn *websocket.Conn, accid prim
|
||||
cmd := make([]byte, size[0])
|
||||
r.Read(cmd)
|
||||
|
||||
result, err := ws.Call(peer, string(cmd), r)
|
||||
result, err := ws.call(peer, string(cmd), r)
|
||||
|
||||
if err != nil {
|
||||
response[0] = 21 // 21 : Negative Ack
|
||||
@ -230,14 +234,14 @@ func (ws *WebsocketPeerHandler[T]) upgrade_core(conn *websocket.Conn, accid prim
|
||||
} else {
|
||||
cmd := make([]byte, flag[0])
|
||||
r.Read(cmd)
|
||||
ws.Call(peer, string(cmd), r)
|
||||
ws.call(peer, string(cmd), r)
|
||||
}
|
||||
}
|
||||
}
|
||||
}(conn, accid)
|
||||
}
|
||||
|
||||
func (ws *WebsocketPeerHandler[T]) upgrade_nosession(w http.ResponseWriter, r *http.Request) {
|
||||
func (ws *websocketPeerHandler[T]) upgrade_nosession(w http.ResponseWriter, r *http.Request) {
|
||||
// 클라이언트 접속
|
||||
defer func() {
|
||||
s := recover()
|
||||
@ -287,7 +291,7 @@ func (ws *WebsocketPeerHandler[T]) upgrade_nosession(w http.ResponseWriter, r *h
|
||||
ws.upgrade_core(conn, accid, nonce)
|
||||
}
|
||||
|
||||
func (ws *WebsocketPeerHandler[T]) upgrade(w http.ResponseWriter, r *http.Request) {
|
||||
func (ws *websocketPeerHandler[T]) upgrade(w http.ResponseWriter, r *http.Request) {
|
||||
// 클라이언트 접속
|
||||
defer func() {
|
||||
s := recover()
|
||||
|
||||
Reference in New Issue
Block a user