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

Commit

Permalink
abstract websockets a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
clif-os committed Sep 7, 2020
1 parent 1632c8c commit 28b6062
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
16 changes: 3 additions & 13 deletions client/src/pages/GroupPhoto/GroupPhoto.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@ import React, { useEffect, useState } from 'react'
import Button from '../../components/Button'
import Page from '../../components/Page'
import { downloadFromS3 } from '../../utils/download'
import { onWsEvent } from '../../websockets'
import './GroupPhoto.css'

const HOST = window.location.origin
.replace(/^http/, 'ws')
.replace('3000', '3001')
const ws = new WebSocket(HOST)

// `${window.location.protocol === 'https:' ? 'wss' : 'ws'}://${
// window.location.hostname
// }:${process.env.PORT || 3001}`,

const GroupPhoto = () => {
const [isLoading, setIsLoading] = useState(true)
const [isGenerating, setIsGenerating] = useState(false)
Expand Down Expand Up @@ -42,16 +34,14 @@ const GroupPhoto = () => {

useEffect(() => {
getGroupPhoto()
ws.onmessage = (event) => {
const data = JSON.parse(event.data)
if (data.id !== 'group-photo') return
onWsEvent('group-photo', (data) => {
if (data.status === 200) {
setFile(data.data)
} else {
console.log(data.message, data.error)
}
setIsGenerating(false)
}
})
}, [])

const header = <h1>Create Group Photo</h1>
Expand Down
12 changes: 12 additions & 0 deletions client/src/websockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const HOST = window.location.origin.replace(/^http/, 'ws')

if (process.env.NODE_ENV === 'development') HOST.replace('3000', '3001')

export const ws = new WebSocket(HOST)

export const onWsEvent = (eventId, callback) => {
ws.onmessage = (event) => {
const data = JSON.parse(event.data)
if (data.id === eventId) callback(data)
}
}
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@
"eslint --fix",
"prettier --write"
]
},
"optionalDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": "^5.0.2"
}
}

0 comments on commit 28b6062

Please sign in to comment.