package ipc
import (
"encoding/json"
"fmt"
)
type Request struct {
Method string "method"
Params string "params"
}
type Response struct {
Code string "code"
Body string "body"
}
type Server interface {
Name() string
Handle(method, params string) *Response
}
type IpcServer struct {
Server
}
func NewIpcServer(server Server) *IpcServer {
return &IpcServer(server)
}
func (server *IpcServer) Connect() chan string {
session := make(chan string, 0)
go func(c chan string) {
for {
request := <-c
if request == "CLOSE" { // 關閉該串連
break
}
var req Request
err := json.Unmarshal([]byte(request), &req)
if err != nil {
fmt.Println("Invalid request format:", request)
}
resp := server.Handle(req.Method, req.Params)
b, err := json.Marshal(resp)
c <- string(b) // 返回結果
}
fmt.Println("Session closed.")
}(session)
fmt.Println("A new session has been created successfully.")
return session
}
return &IpcServer(server)
程式編譯這行報錯,說類型轉換異常。不知怎麼改