javascript 在 nodejs 中传递消息
javascript message passing in nodejs
谁能帮我解决这个问题?每次收到消息我都想把消息从websocket传到串口
var firmata = require('firmata');
var board = new firmata.Board('COM4');
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8081 });
wss.on('connection', function connection(websocket) {
websocket.on('message', function incoming(message) {
board.servoWrite(5, message); //This doesn't work but how do i send?
}
board.on("ready", function() {
board.servoWrite(5, 0); // this works but i need the message from above which I cant access.
});
您应该在 board
准备好后添加监听器。
board.on("ready", function() {
wss.on('connection', function connection(websocket) {
websocket.on('message', function incoming(message) {
board.servoWrite(5, message);
});
});
});
或者声明一个变量,告诉您它是否准备就绪。
var isReady = false;
board.on("ready", function() {
isReady = true;
});
wss.on('connection', function connection(websocket) {
websocket.on('message', function incoming(message) {
if(isReady)
board.servoWrite(5, message);
});
});
这不是语法错误..必须添加
board.servoConfig(5, 0, 30);
我是这样在代码中添加的:
var firmata = require('firmata');
var board = new firmata.Board('COM4',function(){
});
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8081 });
wss.on('connection', function connection(websocket) {
websocket.on('message', function incoming(message) {
board.servoConfig(5, 0, 30);
board.servoWrite(5, message); //This doesn't work but how do i send?
}
board.on("ready", function() {
board.servoWrite(5, 0); // this works but i need the message from above which I cant access.
});
谁能帮我解决这个问题?每次收到消息我都想把消息从websocket传到串口
var firmata = require('firmata');
var board = new firmata.Board('COM4');
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8081 });
wss.on('connection', function connection(websocket) {
websocket.on('message', function incoming(message) {
board.servoWrite(5, message); //This doesn't work but how do i send?
}
board.on("ready", function() {
board.servoWrite(5, 0); // this works but i need the message from above which I cant access.
});
您应该在 board
准备好后添加监听器。
board.on("ready", function() {
wss.on('connection', function connection(websocket) {
websocket.on('message', function incoming(message) {
board.servoWrite(5, message);
});
});
});
或者声明一个变量,告诉您它是否准备就绪。
var isReady = false;
board.on("ready", function() {
isReady = true;
});
wss.on('connection', function connection(websocket) {
websocket.on('message', function incoming(message) {
if(isReady)
board.servoWrite(5, message);
});
});
这不是语法错误..必须添加
board.servoConfig(5, 0, 30);
我是这样在代码中添加的:
var firmata = require('firmata');
var board = new firmata.Board('COM4',function(){
});
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8081 });
wss.on('connection', function connection(websocket) {
websocket.on('message', function incoming(message) {
board.servoConfig(5, 0, 30);
board.servoWrite(5, message); //This doesn't work but how do i send?
}
board.on("ready", function() {
board.servoWrite(5, 0); // this works but i need the message from above which I cant access.
});