同一LAN网络上两台计算机之间的Websocket

时间:2017-06-03 01:39:14

标签: websocket lan

是否可以在两台计算机之间打开websocket,这两台计算机位于同一WAN网络上或通过以太网电缆(LAN)连接?如果是,有人可以提供一些阅读材料,我可以做些什么来学习这个吗?

编辑添加了我的服务器代码

'use strict';

var express = require('express');  //web server
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);   //web socket server

// ===============================Serial port setup========================
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
var parsers = serialport.parsers;
var openSocket = false;

var port = new SerialPort("/dev/ttyACM0", {
    baudrate: 9600,
    parser: parsers.readline('\r\n')
});

// ===============================Server Setup===========================
const netPort = 8080;
server.listen(netPort); //start the webserver on port 8080
app.use(express.static('public')); //tell the server that ./public/ contains 
the static webpages
console.log("listening on port: localhost:" + netPort);

// ==============================Sending data======================
io.sockets.on('connection', function (socket) {
    socket.emit('pi', { status: 'opened' });
    console.log('Socket is open'); // log to console, once serial connection is established
    openSocket = true;
});

port.on('data', function(data) {
    if (openSocket) {
            socket.emit('pi', {value: data}); //send data
    }
});

0 个答案:

没有答案