Skip to content
This repository has been archived by the owner on May 13, 2019. It is now read-only.

Commit

Permalink
Create room in Redis after socket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
alanfriedman committed Dec 11, 2017
1 parent 968d144 commit bd5821c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
14 changes: 1 addition & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,15 @@ app.use(cors({
router.post('/handshake', koaBody, async (ctx) => {
const { body } = ctx.request;
const { roomId } = body;
let ready = false;

let roomExists = await redis.hgetAsync('rooms', roomId)
if (roomExists) {
roomExists = JSON.parse(roomExists)
}

if (!roomExists) {
await redis.hsetAsync('rooms', roomId, JSON.stringify({
id: roomId,
users: [],
isLocked: false,
createdAt: Date.now(),
}))
}

ready = true;

ctx.body = {
id: roomId,
ready,
ready: true,
isLocked: Boolean(roomExists && roomExists.isLocked),
size: ((roomExists && roomExists.users.length) || 0) + 1,
version: config.version,
Expand Down
12 changes: 9 additions & 3 deletions src/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@ export default class Socket {
}

async handleSocket(socket) {
const room = await this.fetchRoom() || {}

socket.on('PAYLOAD', (payload) => {
socket.to(this._roomId).emit('PAYLOAD', payload);
});

socket.on('USER_ENTER', async payload => {
const room = await this.fetchRoom() || {}
let room = await this.fetchRoom()
if (_.isEmpty(room)) {
room = {
id: this._roomId,
users: [],
isLocked: false,
createdAt: Date.now(),
}
}

const newRoom = {
...room,
Expand Down

0 comments on commit bd5821c

Please sign in to comment.