无法从websocket服务器写入浏览器应用

时间:2017-04-18 21:48:56

标签: javascript html node.js websocket serial-port

我相信这是一个简单的问题:我有一个websocket服务器(index.js),它在加载浏览器(index.html)时打开一个串口。我有一个通过USB连接的刻度(COM3)。从浏览器我想将命令发送到比例并将数据接收回浏览器。我的节点版本是v7.7.4,npm版本是4.1.2。我还有NPM安装了serialport和ws。

index.js

    var SerialPort = require('serialport');// include the library
    //var SerialPort = serialport.SerialPort; // make a local instance of it
    var WebSocketServer = require('ws').Server;
    var SERVER_PORT = 8080;               // port number for the webSocket         server
    var wss = new WebSocketServer({port: SERVER_PORT}); // the webSocket server
    var connections = new Array;          // list of connections to the server

    wss.on('connection', handleConnection);
      // get port name from the command line:
       portName = process.argv[2];

    var myPort = new SerialPort(portName, {
       baudRate: 9600,
       // look for return and newline at the end of each data packet:
       parser: SerialPort.parsers.readline("\n")
     });

    myPort.on('open', showPortOpen);
    myPort.on('data', sendSerialData);
    myPort.on('close', showPortClose);
    myPort.on('error', showError);

    // This function broadcasts messages to all webSocket clients
    function broadcast(data) {
     for (myConnection in connections) {   // iterate over the array of connections
      connections[myConnection].send(data); // send the data to each connection
     }
    }

    function sendToSerial(data) {

     myPort.write(" From: index.js:sendToSerial "+data);
     console.log("sendToSerial (index.js): " + data);
     // if there are webSocket connections, send the serial data
       // to all of them:
       if (connections.length > 0) {
         broadcast(data);
       }}

    function handleConnection(client) {

     console.log("New Connection"); // you have a new client
     connections.push(client); // add this client to the connections array

     client.on('message', sendToSerial); // when a client sends a message,

     client.on('close', function() { // when a client closes its connection
         console.log("connection closed"); // print it out
         var position = connections.indexOf(client); // get the client's position in the array
         connections.splice(position, 1); // and delete it from the array
     });
    }


    function showPortOpen() {
       console.log(portName+' port open. Data rate: ' + myPort.options.baudRate);
    }

    function sendSerialData(data) {
     myPort.write(" From: index.js:sendSerialData "+data);
        myPort.write("sendSerialData "+data);
       console.log("sendSerialData "+data);
    }

    function showPortClose() {
       console.log('port closed.');
    }

    function showError(error) {
       console.log('Serial port error: ' + error);
    }

和index.html

    <!DOCTYPE html>
    <html>
     <head>
     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.8/p5.min.js"></script>
     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.8/addons/p5.dom.js"></script>
     <script type="text/javascript">

     var text;     // variable for the text div you'll create
     var socket = new WebSocket("ws://localhost:8080");

    function setup() {

     // The socket connection needs two event listeners:
       socket.onopen = openSocket;
       socket.onmessage = showData;

       // make a new div and position it at 10, 10:
       text = createDiv("xxSensor reading:");
       text.position(10,10);

       }

    function openSocket() {
        text.html("Socket open");
        socket.send("Hello websocket server - from index.html");
      }

    function showData(result) {
        // when the server returns, show the result in the div:
        text.html("Sensor reading: " + result.data);
        xPos = int(result.data);        // convert result to an integer
        text.position(xPos, 10);        // position the text
      }

我尝试过SERVER_PORT = 8081,结果相同。

我能够在cmd“node index.js COM3”中查看index.html中的信息,但命令myPort.write无法访问index.html浏览器。

我明白了:

C:\Users\pmfoo\nodeSerialExample>node index.js COM3
COM3 port open. Data rate: 9600
New Connection
sendToSerial (index.js): Hello websocket server - from index.html
sendSerialData       277.5     g
sendSerialData

其中277.5 g是COM3上的比例输出。

并在浏览器中:

Sensor reading: Hello websocket server - from index.html

我跟着Tom Igoe的教程https://itp.nyu.edu/physcomp/labs/labs-serial-communication/lab-serial-communication-with-node-js/只得到了部分结果。我无法从index.js写入index.html,也无法通过index.js向规模发送命令。 index.js可以看到这个缩放命令(“ON”或“OFF”或“PSN”)。任何人都可以帮我解决这两个沟通问题吗?

0 个答案:

没有答案