Skip to content

Web Socket Interfaces

Tsuyoshi Maeda edited this page Dec 22, 2017 · 13 revisions

Document of Socket.io

This application uses socket.io to get user location in real time.

Endpoint

/game

To connect to the server, write following code.

const socket = io('localhost:8888/game');

Event Type

Client

socket.emit

To emit payload/data, write following code.

payload is an object.

socket.emit('eventType', payload)
  • event types
    • getRooms
      • Note: Return room list.
      • Payload: None.
    • createRoom
      • Note: Create a room and join it.
      • Payload: None. (In server side, Use user's socket.id.)
    • joinRoom
      • Note: Join a specific room.
      • Payload:
        • roomId (Required)
          • type: string
          • Note: You can choose one room id returned from getRooms event.
    • leaveRoom
      • Note: Leave a specific room.
      • Payload:
        • roomId (Required)
          • type: string
          • Note: Set a joining room id.
        • userId (Required)
          • type: string
          • e.g.: '12345', 'abc238kd' or '#/ufaosdifj9'.
          • Note:
            • socket.id, facebook_id and so on. ID should be unique.
            • User data in redis is deleted by userId.
    • updateLocation
      • Note: This event has features of both create and update.
      • Payload
        • id (Optional)
          • type: string
          • e.g.: '12345', 'abc238kd' or '#/ufaosdifj9'.
          • Note: socket.id, facebook_id and so on. ID should be unique. If you don't set id, the server assign socket.id.
        • name (Optional)
          • type: string
          • e.g.: 'Tsuyoshi Maeda' or 'tsuyoshi_maeda'
          • Note: facebook name, socket.id and so on. If you do not put any name, socket.id will be set.
        • iconUrl (Optional)
        • location (Required)
          • type: object
          • e.g.: {latitude: 1111, longitude: -1111}
          • Note: In server, this object is stringified to JSON.
        • roomId (Required)
          • type: string
          • Note: Will emit to all users in the same room.
    • closeGame
      • Note: Close game and disconnect current socket connection.
      • Payload:
        • userId (Required)
          • type: string
          • e.g.: '12345', 'abc238kd' or '#/ufaosdifj9'.
          • Note:
            • socket.id, facebook_id and so on. ID should be unique.
            • User data in redis is deleted by userId.
    • disconnect
      • Note: This event is automatically called when socket is disconnected.

socket.on

To listen to an event, write following code. (It looks like addEventListener.)

socket.on('eventType', callback)

result has error or data. When there is error, data is undefined, on the other hand, when response is success, error is undefined.

  • event types
    • resultGetRooms
      • result:
        • error
        • data
          • rooms: array
    • resultCreateRoom
      • result:
        • error
        • data
          • 'success': string
    • resultJoinRoom
      • result:
        • error
        • data
          • ${socket.id} has joined this room.: string
    • resultLeaveRoom
      • result:
        • error
        • data
          • userId: string
    • resultUpdateLocation
      • result:
        • error
        • data: object
          • id: string
          • name: string
          • iconUrl: string
          • location: object
            • latitude: number
            • longitude: number
Clone this wiki locally