import { Canvas, director, Node, WebView, Widget } from "cc"; import { handleUserData, ISsqChatImpl, SsqUser } from "./ISsqChatImpl"; import { ANDROID } from "cc/env"; export class SsqChatNative implements ISsqChatImpl { private _webView: WebView; private _bondData: any; ensureReady(): Promise { if (!this._webView) { const node = new Node("SsqChat"); const scene = director.getScene(); const canvas = scene.getComponentInChildren(Canvas); if (canvas) node.setParent(canvas.node); const widget = node.addComponent(Widget); widget.isAlignLeft = true; widget.isAlignRight = true; widget.isAlignBottom = true; widget.isAlignTop = true; widget.left = 0; widget.right = 0; widget.top = 0; widget.bottom = 0; widget.updateAlignment(); this._webView = node.addComponent(WebView); this._webView.setJavascriptInterfaceScheme("ssqchat"); //@ts-ignore this._webView.setOnJSCallback((_view, url) => { if (url === 'ssqchat://close') { this._webView.node.active = false; } }); } return null; } bindUser(user: SsqUser | null): void { const hostname = ANDROID ? "app_android" : "app_ios"; this._bondData = handleUserData(user, hostname); } openChat(): void { if (!this._webView) return; if (!this._webView.node.active) this._webView.node.active = true; let link = "http://192.168.1.248:8080/ssqchat.html";//AppKernel.stores.mainStore.onlineChat; if (this._bondData) { let params = JSON.stringify(this._bondData); params = encodeURIComponent(params); link = `${link}?${params}`; } console.log(link); this._webView.url = link; } closeChat(): void { if (!this._webView) return; this._webView.node.active = false; } }