我和我的好友正试图将数据从带有wifi屏蔽的arduino推送到运行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代码时,这就是我们在序列中得到的:https://i.stack.imgur.com/FkyKk.png
欢迎任何意见。。。我们已经为此工作了好几天,但都没有成功。感谢
据我所知,服务器监听端口3000:上的连接
server.listen(3000);
但是您的客户端正在尝试连接到端口80:
if (client.connect("192.168.46.238", 80)) {
试试这个:
if (client.connect("192.168.46.238", 3000)) {