首页 > 解决方案 > Websocket本地ip和公共ip

问题描述

我有一个 webview android 应用程序,它可以通过重定向连接到本地 websocket 和公共 websocket ip 上的服务器,而无需 ip 提供程序。

为了工作,我在我的 html webview 应用程序中有 2 个 js 文件和 2 个 html 文件。像这样:

var ipValue;
var connection;
ipValue = "ws://192.168.1.40:81/";
connection = new WebSocket(ipValue);

 console.log("IP value changed to:"+ipValue);
    connection.onopen = function () {
    connection.send('Websocket funcionando!' + new Date());


    //connection.send('ping');
    //await ws.send('2')
    //ws.send("Hello, Ardunio");
};
connection.onerror = function (error) {
    console.log('WebSocket Error ', error);
};
connection.onmessage = function (e) {
    console.log('Server: ', e.data);

};

for the public 
var ipValue;
var connection;
ipValue = "ws://xxxxxx.ddns.net:81/";
connection = new WebSocket(ipValue);
//console.log(text)
    console.log("IP value changed to:"+ipValue);
    connection.onopen = function () {
    connection.send('Websocket funcionando!' + new Date());
    //connection.send('ping');
    //await ws.send('2')
    //ws.send("Hello, Ardunio");
};
connection.onerror = function (error) {
    console.log('WebSocket Error ', error);
};

如何在同一个js文件中从我的本地IP自动切换到公共IP。或者将 2 个 js 文件合并为一个。

标签: javascript

解决方案


推荐阅读