問題描述:
MQTT.js提供了鏈接MQTT的一套javascipt的方法,可供前端鏈接到MQTT服務器,也能夠做爲腳本測試。
以腳本形式,用nodejs運行,是沒有問題的,可以正常鏈接而且發送報文。
可是若是把js代碼放到HTML文件中,就不能正常完成鏈接,提示:
客戶端提示:前端
服務器提示:java
問題解決;
根據客戶端提示,是沒法完成握手鍊接,根據服務器提示,是由於解包的時候,包不符合格式,致使了鏈接拒絕。
經過查閱文獻,發現普通的socket和websocket是不同的。
直接運行腳本使用的是socket.io 而 瀏覽器使用的是websocket,而處理這兩種報文是不同的。
因此猜想是MQTT並無打開websocket支持,而是把過來的包當作普通的socket包處理了。
查閱代碼發現:
我服務器使用的是moquette,裏面的nettyAcceptor已經明確說明了TCP和websocket的不一樣處理,能夠看到代碼中websocket是關閉的:node
private void initializePlainTCPTransport(final NettyMQTTHandler handler, IConfig props) throws IOException { final MoquetteIdleTimeoutHandler timeoutHandler = new MoquetteIdleTimeoutHandler(); String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME); String tcpPortProp = props.getProperty(PORT_PROPERTY_NAME, DISABLED_PORT_BIND); if (DISABLED_PORT_BIND.equals(tcpPortProp)) { LOG.info("tcp MQTT is disabled because the value for the property with key {}", BrokerConstants.PORT_PROPERTY_NAME); return; } int port = Integer.parseInt(tcpPortProp); initFactory(host, port, new PipelineInitializer() { @Override void init(ChannelPipeline pipeline) { pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT)); pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler); // pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR)); pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector)); pipeline.addLast("decoder", new MQTTDecoder()); pipeline.addLast("encoder", new MQTTEncoder()); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); // pipeline.addLast("messageLogger", new MQTTMessageLogger()); pipeline.addLast("handler", handler); } }); } private void initializeWebSocketTransport(final NettyMQTTHandler handler, IConfig props) throws IOException { String webSocketPortProp = props.getProperty(WEB_SOCKET_PORT_PROPERTY_NAME, DISABLED_PORT_BIND); if (DISABLED_PORT_BIND.equals(webSocketPortProp)) { //Do nothing no WebSocket configured LOG.info("WebSocket is disabled"); return; } else{ LOG.info("WebSocket is enable"); } int port = Integer.valueOf(webSocketPortProp); final MoquetteIdleTimeoutHandler timeoutHandler = new MoquetteIdleTimeoutHandler(); String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME); initFactory(host, port, new PipelineInitializer() { @Override void init(ChannelPipeline pipeline) { pipeline.addLast("httpEncoder", new HttpResponseEncoder()); pipeline.addLast("httpDecoder", new HttpRequestDecoder()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler("/mqtt", MQTT_SUBPROTOCOL_CSV_LIST)); pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder()); pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder()); pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT)); pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler); pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector)); pipeline.addLast("decoder", new MQTTDecoder()); pipeline.addLast("encoder", new MQTTEncoder()); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("handler", handler); } }); }
解決方法:
設置一個端口,我設爲1885,將websocket服務打開就能夠了。web
String webSocketPortProp = props.getProperty(WEB_SOCKET_PORT_PROPERTY_NAME); if (DISABLED_PORT_BIND.equals(webSocketPortProp)) { //Do nothing no WebSocket configured LOG.info("WebSocket is disabled"); return; } else{ LOG.info("WebSocket is enable"); } int port = Integer.valueOf(webSocketPortProp);