我如何从Android创建webScoket服务器,该服务器可以与在我的android网络视图中呈现的此JS代码进行通信

时间:2018-08-10 09:27:12

标签: javascript java android webview android-webview

以下代码来自我的Javascipt(用Angualr编写),该代码将加载到Android应用程序的Webview中,谁能建议我们如何创建服务器来监听我的客户端代码

let webSocketURL =  "ws://localhost:8080";
webSocket:any;
message = '';
try {
    this.webSocket = new WebSocket(webSocketURL);

    this.webSocket.onopen = (openEvent)=>{
        this.message = "WebSocket OPEN";
            this.webSocket.send('hello from client');
        };
        this.webSocket.onclose =  (closeEvent) =>{
            this.message = "WebSocket CLOSE";
        };
        this.webSocket.onerror = (errorEvent) => {
            this.message = "WebSocket ERROR";
        };
        this.webSocket.onmessage = (messageEvent) =>{
            this.message = "Got message from server is "+ messageEvent;
            console.log('messageEvent',messageEvent);
        };
    } catch (exception) {
        console.error(exception);
        this.message = " Got exception" + exception ;
    }

在HTML页面中,我将打印“消息”变量

<h2>Here your message {{message}} </h2>

0 个答案:

没有答案