generated from vivid-lapin/ts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMain.ts
192 lines (189 loc) · 5.93 KB
/
Main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { Server } from "net"
import { networkInterfaces } from "os"
import Router from "@koa/router"
import Bonjour from "bonjour-service"
import type { Service as BonjourServcie } from "bonjour-service"
import { eventmit } from "eventmit"
import getPort from "get-port"
import Koa from "koa"
import ws from "koa-easy-ws"
import {
RMCN_GET_PORT,
RMCN_META,
RMCN_ON_SET_STATE,
RMCN_SET_CP_STATE,
RMCN_SET_SERVICES,
} from "./constants"
import {
ContentPlayerPlayingContent,
InitPlugin,
Service,
} from "../@types/plugin"
export const Main: InitPlugin["main"] = async ({ appInfo, packages }) => {
let server: Server | null = null
let bonjour: Bonjour | null = null
return {
...RMCN_META,
setup: async () => {
let service: BonjourServcie | null = null
const port = await getPort({ port: 10171 }) // TODO: Configurable
try {
bonjour = new Bonjour()
service = bonjour.publish({
name: "miraktest-rmcn",
host: "miraktest-rmcn.local",
type: "http",
port,
txt: appInfo,
})
console.info("[rmcn] Published Bonjour service:", service)
} catch (error) {
console.warn(error, "[rmcn] Bonjour not supported")
}
packages.Electron.ipcMain.handle(RMCN_GET_PORT, () => {
let ip = "unknown"
if (service && service.host) {
ip = service.host
} else {
try {
const addr = Object.values(networkInterfaces())
.flat()
.find((i) => i && i.family === "IPv4" && !i.internal)?.address
if (addr) {
ip = addr
}
} catch {}
}
return `${ip}:${port}`
})
type ContentPlayerState = {
windowId: number
isPlaying: boolean
isSeekable: boolean
playingContent: ContentPlayerPlayingContent | null
time: number
volume: number
}
let activeWindowId = -1
const contentPlayerMap = new Map<string, ContentPlayerState>()
const contentPlayerStateChange = eventmit<ContentPlayerState>()
packages.Electron.ipcMain.handle(
RMCN_SET_CP_STATE,
(
_,
state: {
windowId: number
isPlaying: boolean
isSeekable: boolean
time: number
playingContent: ContentPlayerPlayingContent | null
activeWindowId: number | null
volume: number
}
) => {
contentPlayerMap.set(state.windowId.toString(), state)
contentPlayerStateChange.emit(state)
activeWindowId = state.activeWindowId || -1
}
)
const services = new Map<string, Service>()
packages.Electron.ipcMain.handle(
RMCN_SET_SERVICES,
(_, _services: Service[]) => {
_services.forEach((service) => {
services.set(service.id.toString(), service)
})
}
)
const koa = new Koa()
koa.use(ws())
const router = new Router()
router.use(async (ctx, next) => {
ctx.set("Access-Control-Allow-Origin", "*")
await next()
})
router.options("(.*)", async (ctx) => {
ctx.set("Access-Control-Allow-Methods", "OPTIONS, GET")
ctx.set("Access-Control-Allow-Headers", "*")
ctx.body = ""
ctx.status = 204
})
koa.use(async (ctx, next) => {
if (ctx.ws) {
const ws: WebSocket = await ctx.ws()
const contentPlayerStateChangeHandler = (
state: ContentPlayerState
) => {
ws.send(JSON.stringify({ type: "stateUpdate", ...state }))
}
contentPlayerStateChange.on(contentPlayerStateChangeHandler)
ws.addEventListener("message", async (msg: MessageEvent) => {
try {
const data = JSON.parse(msg.data)
switch (data.type) {
case "ping": {
ws.send(JSON.stringify({ type: "pong" }))
break
}
case "init": {
packages.Electron.browserWindow.getAllWindows()
const initData = {
type: "init",
version: RMCN_META.version,
appVersion: appInfo.version,
contentPlayers: Array.from(contentPlayerMap.values()),
services: Array.from(services.values()),
activeWindowId,
}
ws.send(JSON.stringify(initData))
break
}
case "setState": {
const { key, value } = data as { key: string; value: unknown }
const windowId = parseInt(data.windowId)
if (Number.isNaN(windowId)) {
break
}
const window =
packages.Electron.browserWindow.fromId(windowId)
if (!window) {
break
}
window.webContents.send(RMCN_ON_SET_STATE, { key, value })
break
}
default: {
break
}
}
} catch (error) {
console.error(error)
}
})
ws.onclose = () => {
contentPlayerStateChange.off(contentPlayerStateChangeHandler)
}
} else {
await next()
}
})
koa.use(router.routes())
server = koa.listen(port, () => {
console.info(
"RMCN API Server listening on port http://localhost:" + port
)
})
},
destroy: () => {
if (server) {
server.close()
}
if (bonjour) {
bonjour.destroy()
}
packages.Electron.ipcMain.removeHandler(RMCN_GET_PORT)
packages.Electron.ipcMain.removeHandler(RMCN_SET_CP_STATE)
packages.Electron.ipcMain.removeHandler(RMCN_SET_SERVICES)
},
}
}