1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| <template> <view class="content"> <image class="logo" src="/static/logo.png"></image> <view class="text-area"> <text class="title">{{title}}</text> </view> </view> <view> <view class="title">目标ip</view> <input style="background-color: antiquewhite;" v-model="udpHost" /> </view> <view> <view class="title">目标端口</view> <input style="background-color: antiquewhite;" v-model="udpPort"/> </view> <view> <view class="title">发送消息</view> <input style="background-color: antiquewhite;" v-model="udpMessage"/> </view> <button style="margin-top: 50px;" @click="btnStart"> {{buttonLabel}} </button> </template>
<script setup> import { onMounted, ref } from "vue";
const buttonLabel = ref("点我发送消息"); const title = "小测试咯";
const udpHost = ref('255.255.255.255'); const udpPort = ref(60000); const udpMessage = ref('aa0001b3'); const udpGetPort = 60222; const udpTimeout = 6000;
function udptest(hexstr) { var DatagramPacket = plus.android.importClass('java.net.DatagramPacket'); var DatagramSocket = plus.android.importClass('java.net.DatagramSocket'); var InetAddress = plus.android.importClass('java.net.InetAddress'); var NetworkInterface = plus.android.importClass('java.net.NetworkInterface'); var JString = plus.android.importClass('java.lang.String');
var socket; var address = udpHost.value; var port = Number(udpPort.value); var getPort = udpGetPort; var timeout = udpTimeout;
try { if (DatagramSocket == undefined) { return; } var udpip = InetAddress.getByName(address); socket = new DatagramSocket(getPort); socket.setSoTimeout(timeout);
let sendData = HexString2Bytes(hexstr); let sendPacket = new DatagramPacket(sendData, sendData.length, udpip, port); socket.send(sendPacket); console.log('指令发送完成') } catch (ex) { console.log('========出错了=======', ex); } finally { if (socket != undefined) { socket.close(); } } }
function HexString2Bytes(str) { var pos = 0; var len = str.length; if (len % 2 != 0) { return null; } len /= 2; var arrBytes = new Array(); for (var i = 0; i < len; i++) { var s = str.substr(pos, 2); var v = intToByte(parseInt(s, 16)); arrBytes.push(v); pos += 2; } return arrBytes; }
function intToByte(i) { var b = i & 0xFF; var c = 0; if (b >= 128) { c = b % 128; c = -1 * (128 - c); } else { c = b; } return c; }
function btnStart() { let hexstr = udpMessage.value; udptest(hexstr); } </script>
<style> .content { display: flex; flex-direction: column; align-items: center; justify-content: center; }
.logo { height: 200rpx; width: 200rpx; margin-top: 200rpx; margin-left: auto; margin-right: auto; margin-bottom: 50rpx; }
.text-area { display: flex; justify-content: center; }
.title { font-size: 36rpx; color: #8f8f94; } </style>
|