From a1e696ff09103f0ff96596c7cf413a822340f203 Mon Sep 17 00:00:00 2001 From: rudolfkoenig <> Date: Sat, 18 Feb 2023 21:22:32 +0000 Subject: [PATCH] MQTT2_CLIENT: MQTT over websocket Work-In-Progress (Forum #111959) git-svn-id: https://svn.fhem.de/fhem/trunk@27247 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/00_MQTT2_CLIENT.pm | 20 ++++++++++++++++++++ fhem/FHEM/DevIo.pm | 5 +++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/fhem/FHEM/00_MQTT2_CLIENT.pm b/fhem/FHEM/00_MQTT2_CLIENT.pm index 21cc28ae0..00532ce5b 100644 --- a/fhem/FHEM/00_MQTT2_CLIENT.pm +++ b/fhem/FHEM/00_MQTT2_CLIENT.pm @@ -43,6 +43,7 @@ MQTT2_CLIENT_Initialize($) disable:1,0 disabledForIntervals disconnectAfter + httpHeader ignoreRegexp lwt lwtRetain @@ -107,6 +108,10 @@ MQTT2_CLIENT_connect($) $hash->{connecting} = 1 if($disco && !$hash->{connecting}); $hash->{nextOpenDelay} = 5; $hash->{BUF}=""; + if($hash->{DeviceName} =~ m/^wss?:/) { + $hash->{binary} = 1; + $hash->{header}{"Sec-WebSocket-Protocol"} = "mqtt"; + } return DevIo_OpenDev($hash, $disco, "MQTT2_CLIENT_doinit", sub(){}) if($hash->{connecting}); } @@ -314,6 +319,14 @@ MQTT2_CLIENT_Attr(@) } } + if($attrName eq "httpHeader") { + $hash->{header} = {}; + for my $kv (split(" ",$param[0])) { + my ($k, $v) = split(":", $kv, 2); + $hash->{header}{$k} = $v; + } + } + my %h = (clientId=>1,lwt=>1,lwtRetain=>1,subscriptions=>1,SSL=>1,username=>1); if($init_done && $h{$attrName}) { delete($hash->{authError}); @@ -874,6 +887,13 @@ MQTT2_CLIENT_feedTheList($$$) inactivity, and will be automatically reopened when sending a command.
+ +
  • header
    + a list of space separated tuples of key:value, used to set the HTTP + header when MQTT is used over websocket. +

  • + +
  • ignoreRegexp
    if $topic:$message matches ignoreRegexp, then it will be silently ignored. diff --git a/fhem/FHEM/DevIo.pm b/fhem/FHEM/DevIo.pm index d7bc388f1..99f6cfce0 100644 --- a/fhem/FHEM/DevIo.pm +++ b/fhem/FHEM/DevIo.pm @@ -280,8 +280,9 @@ DevIo_SimpleWrite($$$;$) } elsif($hash->{TCPDev}) { if($hash->{WEBSOCKET}) { - $msg = Encode::encode('UTF-8', $msg) if($unicodeEncoding && !$hash->{binary}); - $msg = DevIo_MaskWS($hash->{binary} ? 0x2:0x1, $msg) + $msg = Encode::encode('UTF-8', $msg) + if($unicodeEncoding && !$hash->{binary}); + $msg = DevIo_MaskWS($hash->{binary} ? 0x2:0x1, $msg); } syswrite($hash->{TCPDev}, $msg);