谢谢你们数以百万计的人。
我在客户端出现了新的错误。下面是我的服务器端代码。当我运行此代码时。我可以在浏览器中看到错误。 whic是:在收到握手响应之前关闭连接
我不知道如何解决它。我对这种事情很新。我很抱歉,但反刍提出任何建议吗?万分感谢:)
var express = require('express');
var app= express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var fs = require('fs');
app.listen(8888, function(){ console.log("running bro");});
app.use(express.static(__dirname));
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.on('turnon', function (data) {
console.log(data.turnon);
//serialPort.write("on");
});
socket.on('turnoff', function(data){
console.log(data.turnoff);
// serialPort.write("off");
});
});

<!DOCTYPE HTML>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<meta charset="utf-8">
<meta name="description" content="Web to serial Arduino">
<title>Web to serial Arduino</title>
<script src="socket.io/node_modules/socket.io-client/socket.io.js"></script>
<script>
//var socket = io.connect('http://localhost:8080');
var socket = io('http://localhost:8080', {
'transports': ['websocket', 'polling']
});
</script>
</head>
<body>
<div class="banner top">
<a href="index.html">
<img src="Drawing.png" alt="logo" class="sitelogo">
</a>
<div class="w3-right toptext w3-wide">An Arduino project for robotic Arm</div>
</div>
<div class="fullscreen background" style="background-image:url('http://cdns.nocamels.com/wp-content/uploads/2013/10/bigstock-Business-technologies-today-43292197.jpg');" data-img-width="1600" data-img-height="1064">
<div class="content-a">
<div class="content-b">
<br>WELCOME TO Arduino "A simple function to test node.js"
<br>
<div class="button" onclick="socket.emit('turnon', { turnon:'on'});">
Turn On
</div> <span style="color: #ffffff;"><a class="button primary-button" onclick="socket.emit('turnoff', {turnoff:'off'});">Turn off</a> </span>
<br>
<div class="button" onclick="console.log('connected');">
connect
</div> <span style="color: #ffffff;"><a class="button primary-button" onclick="console.log('reset');">Reset</a> </span>
</div>
</div>
</div>
</body>
</html>
&#13;
答案 0 :(得分:0)
您在服务器(8888)和客户端(8080)中使用两个不同的端口,您应该使用相同的端口。