Skip to content

Latest commit

 

History

History
207 lines (203 loc) · 3.49 KB

README.md

File metadata and controls

207 lines (203 loc) · 3.49 KB

About this

Backend for mobile app dev project(wachet)

Run

yarn && yarn serve


HTTP Api

Listen port 3000

Login

  • Request
    Url: /login
    Method: POST
    Body:
    ### x-www-form-urlencoded formated ###
    {
      "name": string,
      "pwd": string
    }
    where pwd has been hashed
  • Response
    HTTP code: 200 OK | 404 Not Found
    Body:
    {
      "id": number,
      "token": string,
      "expireTime": timestamp
    }
    browser will be set cookie here
    note: token used to build websocket connection

Logout

  • Request
    Url: /logout
    Method: POST
    Body: no body
  • Response
    HTTP code: 204 No Content | 403 Forbidden
    Body: no body

Friends

  • Request
    Url: /friends
    Method: GET
    Body: no body
  • Response
    HTTP code: 200 OK | 403 Forbidden
    Body:
    [
      {
        "id": number,
        "name": string,
        "avatar": string
      }
    ]

Channels

  • Request
    Url: /channels
    Method: GET
    Body: no body
  • Response
    HTTP code: 200 OK | 403 Forbidden
    Body:
    [
      {
        "name": string,
        "users": [
          {
            "id": number,
            "name": string,
            "avatar": string
          }
        ]
      }
    ]

Websocket API

After login, client creates a websocket connection with server
Listen port 3001

Hello

  • Client
    {
      "action": "hello",
      "msg": {
        "token": string
      }
    }
    all actions should be uncapitalized
    note: token gotten from login API
  • Server
    {
      "action": "hi",
      "msg": {
        "id": number,
        "name": string,
        "avatar": string
      }
    }
    this response means server authorized user

New chat

  • Client
    {
      "action": "new-chat",
      "msg": {
        "to": [
          {
            "id": number
          }
        ]
      }
    }
    id is a user id
  • Server
    to Client:
    {
      "action": "new-chat-res",
      "msg": {
        "channel": string
      }
    }
    returns created channel

Receive Message(Client Side) / Pull Message(Server Side)

  • Server
    {
      "action": "pull-message",
      "msg": {
        "messages": [
          IMessageType
        ]
      }
    }
    IMessageType is defined as:
    {
      "uuids": [
        {
          "user": number,
          "uuid": number
        }
      ],
      "sender": {
        "id": number,
        "name": string,
        "avatar": string
      },   // user
      "msgId": number,
      "channel": string,
      "content": string,
      "time": timestamp
    }
  • Client
    {
      "action": "client-received",
      "msg": {
        "uuids": [number]
      }
    }
    sequence is to distinguish which message client received

Send Message(Client Side) / Push Message(Server Side)

  • Client
    {
      "action": "push-message",
      "msg": {
        "sequence": number,
        "channel": string,
        "content": string
      }
    }
  • Server
    {
      "action": "server-received",
      "msg": {
        "sequence": number,
        "time": timestamp,
        "msgId": number
      }
    }
    where sequence is to distinguish wich message has been received

Any Error Definations

  • forbidden: No access to current channel
  • invalid-param: Param invalid
  • token-missing: Token given missing or incorrect