javascript - 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 文件合并为一个。
解决方案
推荐阅读
- python - 如何使用 Beautifulsoup 从 HTML 标签中获取单个属性的值?
- ios - React-Native ENOENT:没有这样的文件或目录,在 Xcode 中打开'assets-library://asset/asset.jpg'
- javascript - 如何在每个 Datatable Table tr 上添加 onclick 函数?
- react-native - React Native Detox 硬件限制?
- java - 在春季验证接受标头
- javascript - 不能在particle.js上放任何东西
- css - 选中复选框时设置元素样式
- mongodb - 在 MongoDB 中的数组的相同元素中查找 2 个或多个条件
- java - Spring Cloud Stream 绑定可视化
- ios - 下载PDF文件并保存在文档目录中