func (wsc *WebsocketController) Server(ws *websocket.Conn) { defer func() { var reqJson ReqJsonRpc wsc.Logout(ws, reqJson) }() for { // 消息监听接收, 验证 var reply string if err := websocket.Message.Receive(ws, &reply); err != nil { fmt.Println("无法接收.") // response and close break } var reqJson ReqJsonRpc if err := json.Unmarshal([]byte(reply), &reqJson); err != nil { fmt.Println("响应: ", err.Error()) // response and don't close } dbgutil.FormatDisplay("reqJson", reqJson) // 方法对应的操作 if reqJson.Method == "login" { wsc.Login(ws, reqJson) } if reqJson.Method == "logout" { wsc.Logout(ws, reqJson) } if reqJson.Method == "sendto" { wsc.Sendto(ws, reqJson) } } }
func Run() { // 路由配置 App.Router.StaticDir = GetConfig("ROUTE_STATIC_DIR") App.Router.StaticPath = GetConfig("ROUTE_STATIC_PATH") // 视图配置 App.Viewer.ViewDir = GetConfig("VIEW_PATH") dbgutil.FormatDisplay("App", App) port := GetConfig("HTTP_PORT") http.ListenAndServe(":"+port, App.Router) }