Skip to content

Commit

Permalink
clean sockets logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TCWTEAM committed Apr 9, 2021
1 parent 8b382a0 commit 209155a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 61 deletions.
61 changes: 0 additions & 61 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import {
const electron = require('electron')
const path = require('path')
const localStore = createLocalStore()
const WebSocket = require('ws')
const fs = require('fs')

let blacklistSites = []

let timerState = null

/**
* Set `__static` path to static files in production
Expand Down Expand Up @@ -121,21 +115,6 @@ ipcMain.on('reload-global-shortcuts', (event, shortcuts) => {
loadGlobalShortcuts(shortcuts)
})

ipcMain.on('round-change', (event, round) => {
timerState = round

wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({
event: 'round-change',
data: {
state: timerState
}
}))
}
})
})

function getNewWindowPosition() {
const windowBounds = mainWindow.getBounds()
const trayBounds = tray.getBounds()
Expand Down Expand Up @@ -230,46 +209,6 @@ function loadGlobalShortcuts(globalShortcuts) {
})
}

if (process.env.POMOTROID_LIST) {
const blacklistContents = fs.readFileSync(process.env.POMOTROID_LIST, 'utf8')

blacklistSites = blacklistContents.split('\n')
}

// eslint-disable-next-line no-new
const wss = new WebSocket.Server({
port: 8080

})

wss.on('message', (data) => {
logger.info(`New Websocket Message ${data}`)
})

wss.on('error', (err) => {
logger.error(err)
})

wss.on('connection', (ws) => {
logger.info('New Websocket Connection')

ws.on('message', (data) => {
if (data === 'get-current-state') {
ws.send(JSON.stringify({
event: 'round-change',
data: {
state: timerState
}
}))
} else if (data === 'get-blacklist-sites') {
ws.send(JSON.stringify({
event: 'blacklist-sites',
data: blacklistSites
}))
}
})
})

/**
* Auto Updater
*
Expand Down
97 changes: 97 additions & 0 deletions src/main/sockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const WebSocket = require('ws')
import { ipcMain } from 'electron'

let wss
let timerState = null

// Events
const GET_STATE = 'state'
const ROUND_CHANGE = 'roundChange'

/* Handle a round change from the renderer */
ipcMain.on(ROUND_CHANGE, (_event, round) => {
timerState = round

const message = {
event: ROUND_CHANGE,
data: {
state: timerState
}
}

sendGlobalMessage(message)
})

/**
* Handle an incoming websocket message
* @param {WebSocketClient} ws - The client instance
* @param {string} data - The raw data sent to the websocket
*/
export const handleMessage = (ws, data) => {
const parsedData = JSON.parse(data)

switch (parsedData.event) {
case GET_STATE:
const response = {
event: GET_STATE,
data: {
state: timerState
}
}

sendMessage(ws, response)
break
default:
}
}

/**
* Send a websocket message to every connected client
* @param {Object} data - Data to send to every connected client
*/
export const sendGlobalMessage = data => {
const parsedData = JSON.stringify(data)

wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(parsedData)
}
})
}

/**
* Send a websocket message to an individual client
* @param {WebSocketClient} ws - The client instance
* @param {Object} data - Data to send to the client
*/
export const sendMessage = (ws, data) => {
const parsedData = JSON.stringify(data)

ws.send(parsedData)
}

/**
* Initialize a local websocket instance and establish handlers
* @param {Number} port - The port to run the websocket on
*/
export const init = port => {
wss = new WebSocket.Server({
port
})

wss.on('message', data => {
logger.info(`New Websocket Message ${data}`)
})

wss.on('error', err => {
logger.error(err)
})

wss.on('connection', ws => {
logger.info('New Websocket Connection')

ws.on('message', data => {
handleMessage(ws, data)
})
})
}

0 comments on commit 209155a

Please sign in to comment.