【问题标题】:Invalid handshake - arduino to nodejs server with socket.io无效的握手 - arduino 到带有 socket.io 的 nodejs 服务器
【发布时间】:2015-05-05 11:05:54
【问题描述】:

我和我的伙伴正在尝试将数据从我们的 arduino 与 wifi shield 推送到我们运行 socket.io (v1.3.4) 框架和 express (v4.12.0) 框架的 nodejs 本地服务器。

我们尝试了多个库并且已经关闭,但现在在尝试将我们的 http 连接升级到 websocket 连接时出现“无效握手”错误。任何想法我们做错了什么?尝试连接 echo.websocket.org 服务确实有效,因此我们假设服务器端缺少某些东西。 (使用 chrome 或其他浏览器时一切正常)

Node.js 服务器代码:

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server),
    seats = [
        {id: 0, status: false},
        {id: 1, status: false},
        {id: 2, status: false},
        {id: 3, status: false},
        {id: 4, status: false},
        {id: 5, status: false},
        {id: 6, status: false},
        {id: 7, status: false},
        {id: 8, status: false},
        {id: 9, status: false},
        {id: 10, status: false},
        {id: 11, status: false},
        {id: 12, status: false},
        {id: 13, status: false},
        {id: 14, status: false},
        {id: 15, status: false}
    ]; //Seats from id 0 --> 15

server.listen(3000);

app.use("/css", express.static(__dirname + '/css'));
app.get('/', function(req, res) {
    res.sendFile(__dirname + '/');
});

// Socket code
io.sockets.on('connection', function (socket) {

    console.log("Somebody connected, push seatlist");
    socket.emit('init-seats', seats);

    // Change status of zetel
    socket.on('zetel-toggle', function (data) {
        console.log("zetel-toggle called");
        seats[data.id].status = data.status;
        io.sockets.emit('change-status', data);
    });

    socket.on('hello', function() {
        console.log("Somebody said hallo");
    });


    socket.on('disconnect', function (data) {
        console.log("Somebody DISconnected");
    });
});

/*io.use(function(socket, next) {
  var handshakeData = socket.request;
    for( variabele in handshakeData) {
        console.log("handshakedata: "+variabele);
    }
  next();
});*/

Arduino 代码:

#include <SPI.h>
#include <WiFi.h>
// Here we define a maximum framelength to 64 bytes. Default is 256.
#define MAX_FRAME_LENGTH 64
// Define how many callback functions you have. Default is 1.
#define CALLBACK_FUNCTIONS 1
#include <WebSocketClient.h>

WiFiClient client;
WebSocketClient webSocketClient;
char ssid[] = "#####"; // Blanked out for stackoverflow
char pass[] = "#####"; // Blanked out for stackoverflow
int status = WL_IDLE_STATUS;
int fsrReading;
int fsrAnalogPin = 1;
int LEDbrightness;

void setup() {
  Serial.begin(9600);
  Serial.println(F("Joining WiFi network..."));
  connectWifi();
  pinMode(13, OUTPUT);

  // Connect to the websocket server
  if (client.connect("192.168.46.238", 3000)) {
  //if (client.connect("echo.websocket.org", 80)) {
    Serial.println("Connected");
  } else {
    Serial.println("Connection failed.");
  }

  // Handshake with the server
  webSocketClient.path = "/";
  webSocketClient.host = "192.168.46.238";

  if (webSocketClient.handshake(client)) {
    Serial.println("Handshake successful");
  } else {
    Serial.println("Handshake failed."); 
  }
}

void loop() {
  String data;
  fsrReading = analogRead(fsrAnalogPin);
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);  
  LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
  analogWrite(13, LEDbrightness);

  if (client.connected()) {

    data = webSocketClient.getData();

    if (data.length() > 0) {
      Serial.print("Received data: ");
      Serial.println(data);
    }

    // capture the value of analog 1, send it along
    pinMode(1, INPUT);
    data = String(LEDbrightness);
    if(LEDbrightness > 200) {
      webSocketClient.sendData("hello");
    } else {    
      webSocketClient.sendData("bye");
    }

  } else {

    Serial.println("Client disconnected.");
    while (1) {
      // Hang on disconnect.
    }
  }

  // Wait to fully let the client disconnect
  delay(500);
}

void connectWifi(void) {
  Serial.println("Attempting to connect to Y&RGroup-visitors using WPA2-Personal ...");
  status = WiFi.begin(ssid, pass);
  delay(5000); // give it at least 10 seconds to attempt a connection

  if(status != WL_CONNECTED) {
    Serial.println("Couldn't establish a wireless connection.");
    Serial.println("Network capabilities have been disabled.");
  } else {
    Serial.println("Wireless connection established.");
    printWifiStatus();
  }  
}

void printWifiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

当我们运行 arduino 代码时,这就是我们在串行中得到的: http://i.stack.imgur.com/FkyKk.png

欢迎任何意见... 我们已经为此工作了好几天,但没有成功。 谢谢

【问题讨论】:

  • 有什么更新吗?您是否设法让它工作或任何替代方案?
  • socket.io和arduino的websocket客户端的问题是不兼容。 Socket.io 是 websockts 之上的协议,所以不一样。您甚至无法将简单的 websocket 客户端从 javascript 连接到 socket.io 服务器。

标签: node.js websocket socket.io arduino handshake


【解决方案1】:

据我了解,服务器在 3000 端口监听连接:

server.listen(3000);

但是您的客户端正在尝试连接到端口 80:

if (client.connect("192.168.46.238", 80)) {

试试这个:

if (client.connect("192.168.46.238", 3000)) {

【讨论】:

  • 对不起,当我们切换到 echo.websocket.org 测试时,这是一个复制粘贴错误...当我们尝试连接到本地服务器时,我们使用了端口 3000 - 在 OP 中更改
猜你喜欢
  • 1970-01-01
  • 2022-01-23
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多