initMulticast method
Implementation
Future initMulticast() async {
var sockets = await _getSockets('224.0.0.167', 53318);
stateMessage.value.hardware = StateMessageHardware(
networkInterface: [],
);
for (final socket in sockets) {
var interface = socket.interface;
stateMessage.value.hardware!.networkInterface!
.add(StateMessageHardwareNetworkInterface(
name: interface.name,
ip: interface.addresses.map((e) => e.address).toList().first,
type: interface.name,
));
socket.socket.listen((event) {
if (event == RawSocketEvent.read) {
var datagram = socket.socket.receive();
if (datagram == null) return;
var message = utf8.decode(datagram.data);
try {
final data = jsonDecode(message);
if (data is Map<String, dynamic>) {
if (data['appName'] == null) return;
var serverInfo = MqttServerInfo.fromJson(data);
if (connected.value == false) {
log("[UDP组播消息] $serverInfo");
eventBus.fire(
MqttServerInfoCollected(
MqttClientWrapper(
host: serverInfo.localIp!,
port: int.parse(serverInfo.mqttServerPort!),
clientId: "workSide_${deviceId.value}",
willTopic: "workSide/state/${deviceId.value}",
willMessage: jsonEncode(willStateMessage.value.toJson()),
enableLogging: false,
),
),
);
}
}
} catch (e) {
log("消息解析失败", error: e);
}
}
});
}
}