Add streming class

This commit is contained in:
kPherox 2019-04-24 05:13:32 +09:00
parent ab37afb4e6
commit 088599f88c
No known key found for this signature in database
GPG Key ID: C04751C2BFA2F62D
2 changed files with 86 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import {
import Window from './Window'
import Timeline from './Timeline'
import Streaming from './Streaming'
const isDevelopment = process.env.NODE_ENV !== 'production'
@ -49,6 +50,7 @@ export default class Application {
if (!process.env.WEBPACK_DEV_SERVER_URL) createProtocol('app')
Timeline.ready()
Streaming.ready()
Window.Main()
}

84
src/main/Streaming.ts Normal file
View File

@ -0,0 +1,84 @@
import { ipcMain, Event } from 'electron'
import { default as Mastodon, Status, WebSocket } from 'megalodon'
import Window from './Window'
import Client from './Client'
export default class Streaming {
private static _instance: Streaming
public static get shared(): Streaming {
return this._instance || (this._instance = new this())
}
public static ready() {
ipcMain.on('open-streaming', (_: Event, domain: string, type: string) => {
return this.shared.openStreaming(domain, type)
})
}
private sockets: Map<string, WebSocket>
private constructor() {
this.sockets = new Map()
}
private openStreaming(clientName: string, type: string) {
let client: Mastodon
let stream: string
switch (type) {
case 'no-auth':
client = Client.getNoAuthClient(clientName)
stream = 'public:local'
break
default:
return
}
let socketName = clientName + ':' + type
if (this.sockets.has(socketName)) {
return
}
const socket: WebSocket = client.socket('/streaming', stream)
socket.on('connect', () => {
console.log('start streaming:', socketName)
})
socket.on('update', (status: Status) => {
if (Window.windowMap.has('main')) {
Window.windowMap.get('main')!.webContents.send(`update-${clientName}-${type}`, status)
}
})
socket.on('notification', (notification: Notification) => {
console.log(notification)
})
socket.on('delete', (id: number) => {
if (Window.windowMap.has('main')) {
Window.windowMap.get('main')!.webContents.send(`delete-${clientName}-${type}`, id)
}
})
socket.on('error', (err: Error) => {
console.error(err)
})
socket.on('heartbeat', () => {
console.log('thump.')
})
socket.on('close', () => {
console.log('close streaming:', socketName)
})
socket.on('parser-error', (err: Error) => {
console.error(err)
})
this.sockets.set(socketName, socket)
}
}