MQTT
ååãå¥goroutineã§çºçããã¨ã©ã¼ãã³ããªã³ã°ããã¾ãããå ·ä½çã«ã¯ãµãã¹ã¯ã©ã¤ãã¸ã®æ¸ãè¾¼ã¿ã§ã¨ã©ã¼ãçºçããå ´åã«ãµãã¹ã¯ãªãã·ã§ã³ã®åé¤å¦çããããã«ãã¾ãããä»åã¯ã context.Context ã使ã£ã¦goroutineãåæ¢ãããã¨ã§ãã¯ã©ã¤ã¢ã³ããâ¦
ååã®ç¶ããå¥ã®goroutineã§çºçããã¨ã©ã¼ãerror channelã使ã£ã¦ãã³ããªã³ã°ãã¦ã¿ããã¨æãã¾ãããã³ããªã³ã°ã®å¦çã§mapã使ã£ãã®ã§ãããgoroutineã»ã¼ãã«ãããã sync.Map ã使ã£ã¦ã¿ã¾ããã ç®æ¬¡ã handleSub ã§ã¨ã©ã¼ å¥goroutineãåãåâ¦
ååãgoroutine, channel, selectã使ã£ã¦ Broker ãå®è£ ãã¾ããããã® Broker ããµã¼ãã¼ã§å©ç¨ããMQTTã§ã¡ãã»ã¼ã¸ãé éãã¦ã¿ã¾ãã ãã®åã« <-chan chan<- []byte ã¨ããåãèªã¿ã¥ããã®ã§ type ã使ã£ã¦å¯¾å¿ãã¾ãããã®å¾ãèªã¿æ¸ãå¯è½ãªchanneâ¦
ååã¯goroutineã使ã£ã¦ãè¤æ°ã¯ã©ã¤ã¢ã³ãã¨åæã«æ¥ç¶ã§ããããã«ãã¾ãããä»åã¯ãè¤æ°ã®goroutineéãchannelãã¤ãªãã§ã¡ãã»ã¼ã¸ããã¨ãããPublishãããã¡ãã»ã¼ã¸ãå¥ã®Subscribeãã¦ããã¯ã©ã¤ã¢ã³ãã«é éãã¾ããchannelã®ææ権ãselectãâ¦
ååã¾ã§ã§ãã¯ã©ã¤ã¢ã³ããPUBLISHãã¾ãã¯SUBSCRIBEãããã¨ãã§ããããã«ãªãã¾ããã次ã¯ãã¯ã©ã¤ã¢ã³ãããPUBLISHãããã¡ãã»ã¼ã¸ããSUBSCRIBEãã¦ããå¥ã®ã¯ã©ã¤ã¢ã³ãã¸éãå¦çãå®è£ ãã¦ããã¾ãããããã®åã«goroutineã使ã£ã¦ãµã¼ãã¼ãè¤æ°â¦
ååã§ãã¯ã©ã¤ã¢ã³ãããã®PUBLISHãåãåãã¨ããã¾ã§å®è£ ã§ãã¾ãããä»åã¯SUBSCRIBEãåãåãã¨ãããå®è£ ãã¾ãããã¤ãå¼ç¨ãã¦ãå³ã§è¨ãã¨ãå³å´ã®ãComputerãããMobile deviceãããMQTT Brokerã¸ã®éä¿¡ã§ãã å¼ç¨å ï¼https://www.appceleratâ¦
ååã®ç¶ãã§ããhandlerã®ã¨ã©ã¼ãã³ããªã³ã°ããããã¾ãããã®å¾ãmosquitto_clientããPUBLISHãã±ãããèªä½ãµã¼ãã¼ã§åãåããããã«ãã¾ãããæå¾ã«handlerã®ãªãã¡ã¯ã¿ãªã³ã°ã§ Untyped constant declaration ã¨ããconstã®ä¾¿å©ãªä½¿ãæ¹ãç¥ãã¾â¦
ååã®ç¶ãã§ããååã¾ã§ã§ä¸å¿ CONNECTãã±ãããstructã«å¤æããå¦çãã§ãã¾ãããããã§ã¯ã©ã¤ã¢ã³ãããã®CONNECTãã±ããããµã¼ãã¼å´ã§è§£éã§ãã¾ãã ä»åã¯ãCONNECTã«å¯¾ããã¬ã¹ãã³ã¹ã§ããCONNACKã«åãæããã¾ããã¾ããå®éã«ãµã¼ãã¼ã¨ãâ¦
ååã®ç¶ãã§ãã ä»åã¯ãMQTTã®CONNECTãã±ããã®ãã¤ãã¼ãããããã¤ãã¼ããbinaryããã±ã¼ã¸ã使ã£ã¦å®è£ ãã¾ãããã®å¾ãregexããã±ã¼ã¸ã使ã£ã¦å ¥åãã§ãã¯å¦çãæ¸ãã¦ãã¹ããã¾ããGoã§ã¯ãã¹ãã«ãã¬ãã¸ãç°¡åã«åå¾ã§ããããã ã£ãã®ã§ã試â¦
åå㧠func ToFixedHeader(bs []byte) FixedHeader ã¨ããé¢æ°ãå®è£ ãã¾ããããã®é¢æ°ã®å¼æ°ã®ãã§ãã¯ã¨ã¨ã©ã¼ãã³ããªã³ã°ããããã¾ããGoã«ãããã¨ã©ã¼ãã³ããªã³ã°ãå¦ãã§ããããã¨æãã¾ãã ç®æ¬¡ã error CONNECTãã±ããã®å¯å¤ãããã¼ Goã®ã¨â¦
ååãåºå®ãããã¼ã表ãstructã®å®è£ ã«çæãã¾ããã // fixed_header.go package packet type FixedHeader { PacketType byte } func ToFixedHeader(bs []byte) FixedHeader { b := bs[0] packetType := b >> 4 return FixedHeader{packetType} } ãããâ¦
2018å¹´ã¯Goã¨MQTTããã¥ã¼ãããã®ã§ãå¦ãã ãã¨ã®æ¯ãè¿ããå ¼ãã¦ãGoã§MQTTãµã¼ãã¼ãå®è£ ãã¦ã¿ã¾ããã¨ããæ¥è¨ã§ãã 使ç¨ããGoã®ãã¼ã¸ã§ã³ã¯1.10.7ã§ãã MQTTã¨ã¯ä»¥ä¸ã®ããã°PubSubãå®ç¾ãããããã³ã«ã§ãã詳細ã¯å®è£ ããªãã確èªãã¦ããã¾â¦