Example #1
0
func (wh *webSocketHandler) ServeWebSocket(conn *WebConn, r *model.WebSocketRequest) {
	l4g.Debug("/api/v3/users/websocket:%s", r.Action)

	r.Session = *GetSession(conn.SessionToken)
	r.T = conn.T
	r.Locale = conn.Locale

	var data map[string]interface{}
	var err *model.AppError

	if data, err = wh.handlerFunc(r); err != nil {
		l4g.Error(utils.T("api.web_socket_handler.log.error"), "/api/v3/users/websocket", r.Action, r.Seq, r.Session.UserId, err.SystemMessage(utils.T), err.DetailedError)
		err.DetailedError = ""
		errResp := model.NewWebSocketError(r.Seq, err)
		errResp.DoPreComputeJson()

		conn.Send <- errResp
		return
	}

	resp := model.NewWebSocketResponse(model.STATUS_OK, r.Seq, data)
	resp.DoPreComputeJson()

	conn.Send <- resp
}
Example #2
0
func (wr *WebSocketRouter) ServeWebSocket(conn *WebConn, r *model.WebSocketRequest) {
	if r.Action == "" {
		err := model.NewLocAppError("ServeWebSocket", "api.web_socket_router.no_action.app_error", nil, "")
		ReturnWebSocketError(conn, r, err)
		return
	}

	if r.Seq <= 0 {
		err := model.NewLocAppError("ServeWebSocket", "api.web_socket_router.bad_seq.app_error", nil, "")
		ReturnWebSocketError(conn, r, err)
		return
	}

	if r.Action == model.WEBSOCKET_AUTHENTICATION_CHALLENGE {
		token, ok := r.Data["token"].(string)
		if !ok {
			conn.WebSocket.Close()
			return
		}

		session, err := GetSession(token)

		if err != nil {
			conn.WebSocket.Close()
		} else {
			go SetStatusOnline(session.UserId, session.Id, false)

			conn.SessionToken = session.Token
			conn.UserId = session.UserId

			resp := model.NewWebSocketResponse(model.STATUS_OK, r.Seq, nil)
			resp.DoPreComputeJson()
			conn.Send <- resp
			conn.SendHello()
		}

		return
	}

	if !conn.IsAuthenticated() {
		err := model.NewLocAppError("ServeWebSocket", "api.web_socket_router.not_authenticated.app_error", nil, "")
		ReturnWebSocketError(conn, r, err)
		return
	}

	var handler webSocketHandler
	if h, ok := wr.handlers[r.Action]; !ok {
		err := model.NewLocAppError("ServeWebSocket", "api.web_socket_router.bad_action.app_error", nil, "")
		ReturnWebSocketError(conn, r, err)
		return
	} else {
		handler = h
	}

	handler.ServeWebSocket(conn, r)
}
func (wh *webSocketHandler) ServeWebSocket(conn *WebConn, r *model.WebSocketRequest) {
	l4g.Debug("/api/v3/users/websocket:%s", r.Action)

	r.Session = *GetSession(conn.SessionToken)
	r.T = conn.T
	r.Locale = conn.Locale

	data := make(map[string]interface{})

	if err := wh.handlerFunc(r, data); err != nil {
		l4g.Error(utils.T("api.web_socket_handler.log.error"), "/api/v3/users/websocket", r.Action, r.Seq, r.Session.UserId, err.SystemMessage(utils.T), err.DetailedError)
		err.DetailedError = ""
		conn.Send <- model.NewWebSocketError(r.Seq, err)
		return
	}

	conn.Send <- model.NewWebSocketResponse(model.STATUS_OK, r.Seq, data)
}