-
Notifications
You must be signed in to change notification settings - Fork 1
Web Socket Interfaces
Tsuyoshi Maeda edited this page Dec 22, 2017
·
13 revisions
This application uses socket.io to get user location in real time.
- https://socket.io/docs/
- sample code(I just played around with.)
/game
To connect to the server, write following code.
const socket = io('localhost:8888/game');
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.
- roomId (Required)
-
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.
- roomId (Required)
-
updateLocation
- Note: This event has features of both
create
andupdate
. - 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)
- type: string
- e.g.: 'https://example.com/thumbnail.png'
- Note: This images will be used as a marker on google maps.
- 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.
- id (Optional)
- Note: This event has features of both
-
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.
- userId (Required)
-
disconnect
- Note: This event is automatically called when socket is disconnected.
-
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
- result:
-
resultCreateRoom
- result:
- error
- data
- 'success': string
- result:
-
resultJoinRoom
- result:
- error
- data
-
${socket.id} has joined this room.
: string
-
- result:
-
resultLeaveRoom
- result:
- error
- data
-
userId
: string
-
- result:
-
resultUpdateLocation
- result:
- error
- data: object
- id: string
- name: string
- iconUrl: string
- location: object
- latitude: number
- longitude: number
- result:
-