有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript如何从Android创建WebCoket服务器,该服务器可以与我的JS代码进行通信,该代码在我的Android web视图中呈现

下面的代码来自我的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页面中,我将打印“message”变量

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

共 (0) 个答案