|
| 1 | +import Pusher, { Options, Channel } from 'pusher-js' |
| 2 | +import { ClientSentPayloads } from '@/shared/client-sent-payloads' |
| 3 | +import { ServerSentPayloads } from '@/shared/server-sent-payloads' |
| 4 | +import { Document, Paplico, UIStroke } from '@paplico/core-new' |
| 5 | +import { IPaplicoChatBackend } from './IPaplicoChatBackend' |
| 6 | + |
| 7 | +const serialize = (payload: ClientSentPayloads) => JSON.stringify(payload) |
| 8 | + |
| 9 | +export class PaplicoChatPusherBackend implements IPaplicoChatBackend { |
| 10 | + protected pplc: Paplico | null = null |
| 11 | + protected psr: Pusher | null = null |
| 12 | + protected channel: Channel | null = null |
| 13 | + |
| 14 | + protected appKey: string |
| 15 | + protected pusherOptions: Options |
| 16 | + |
| 17 | + constructor(appKey: string, options: Options) { |
| 18 | + this.appKey = appKey |
| 19 | + this.pusherOptions = options |
| 20 | + } |
| 21 | + |
| 22 | + public dispose() { |
| 23 | + this.channel?.disconnect() |
| 24 | + this.psr?.disconnect() |
| 25 | + } |
| 26 | + |
| 27 | + public async connect(pplc: Paplico) { |
| 28 | + this.pplc = pplc |
| 29 | + this.psr = await new Promise<Pusher>((resolve, reject) => { |
| 30 | + const psr = new Pusher(this.appKey, this.pusherOptions) |
| 31 | + resolve(psr) |
| 32 | + }) |
| 33 | + } |
| 34 | + |
| 35 | + public async requestJoin(roomId?: string) { |
| 36 | + const channel = (this.channel = this.psr!.subscribe(`private-${roomId}`)) |
| 37 | + |
| 38 | + this.channel = await new Promise<Channel>((resolve, reject) => { |
| 39 | + channel.bind('pusher:subscription_succeeded', () => { |
| 40 | + resolve(channel) |
| 41 | + }) |
| 42 | + |
| 43 | + channel.bind('pusher:subscription_error', (e) => { |
| 44 | + reject(e) |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + this.channel.bind('pplc-event', (data: string) => { |
| 49 | + const message: ServerSentPayloads = JSON.parse(data) |
| 50 | + |
| 51 | + switch (message.type) { |
| 52 | + case 'joined': { |
| 53 | + const { roomId, layerIds, canvasSize } = message |
| 54 | + |
| 55 | + const doc = Document.visu.createDocument(canvasSize) |
| 56 | + |
| 57 | + layerIds.forEach((layerId: string, idx: number) => { |
| 58 | + const layer = Document.visu.createCanvasVisually({ |
| 59 | + width: canvasSize.width, |
| 60 | + height: canvasSize.height, |
| 61 | + name: `Layer ${idx + 1}`, |
| 62 | + }) |
| 63 | + |
| 64 | + layer.uid = layerId |
| 65 | + doc.layerNodes.addLayerNode(layer) |
| 66 | + }) |
| 67 | + |
| 68 | + this.pplc!.loadDocument(doc) |
| 69 | + this.pplc!.setStrokingTarget([layerIds[0]]) |
| 70 | + |
| 71 | + console.info(`⚡ Joined room: ${roomId}`) |
| 72 | + break |
| 73 | + } |
| 74 | + case 'strokeComplete': { |
| 75 | + console.log('⚡ receive strokeComplete') |
| 76 | + const { |
| 77 | + uiStroke, |
| 78 | + targetLayerUid, |
| 79 | + brushSetting: strokeSettings, |
| 80 | + } = message |
| 81 | + |
| 82 | + this.pplc!.putStrokeComplete( |
| 83 | + Object.assign(new UIStroke(), uiStroke), |
| 84 | + { |
| 85 | + pathToTargetVisu: [targetLayerUid], |
| 86 | + brushSetting: strokeSettings, |
| 87 | + }, |
| 88 | + ) |
| 89 | + break |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + return new Promise<void>((resolve, reject) => { |
| 95 | + const onError = (e: Event) => { |
| 96 | + reject(e) |
| 97 | + } |
| 98 | + |
| 99 | + // this.psr!.addEventListener('open', () => { |
| 100 | + // this.psr!.removeEventListener('error', onError) |
| 101 | + // resolve() |
| 102 | + // }) |
| 103 | + |
| 104 | + // this.psr!.addEventListener('error', onError) |
| 105 | + // }) |
| 106 | + |
| 107 | + // this.psr?.send( |
| 108 | + // serialize({ |
| 109 | + // type: 'join', |
| 110 | + // }), |
| 111 | + // ) |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + public putStrokeComplete( |
| 116 | + data: Omit<ClientSentPayloads.StrokeCompletePayload, 'type'>, |
| 117 | + ) { |
| 118 | + this.psr?.send( |
| 119 | + serialize({ |
| 120 | + type: 'strokeComplete', |
| 121 | + ...data, |
| 122 | + }), |
| 123 | + ) |
| 124 | + } |
| 125 | +} |
0 commit comments