{"id":38844,"date":"2023-10-02T07:26:06","date_gmt":"2023-10-01T23:26:06","guid":{"rendered":"\/\/www.dealsbon.com\/?p=38844"},"modified":"2023-10-02T07:26:06","modified_gmt":"2023-10-01T23:26:06","slug":"nqckrhgdw","status":"publish","type":"post","link":"\/\/www.dealsbon.com\/blog\/38844.html","title":{"rendered":"Node.js\u53d6\u4e32\u53e3\uff0c\u5982\u4f55\u7ed9\u5230Web"},"content":{"rendered":"
Node.js\u662f\u4e00\u4e2a\u57fa\u4e8eJavaScript\u7684\u5f00\u6e90\u670d\u52a1\u5668\u73af\u5883\uff0c\u5b83\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u4f7f\u7528JavaScript\u7f16\u5199\u670d\u52a1\u5668\u7aef\u5e94\u7528\u7a0b\u5e8f\u3002Node.js\u53ef\u4ee5\u63d0\u4f9b\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u5f0f\u6765\u4ece\u4e32\u53e3\u8bbe\u5907\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u5230Web\u5ba2\u6237\u7aef\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Node.js\u53d6\u4e32\u53e3\uff0c\u5e76\u5c06\u6570\u636e\u4f20\u9012\u5230Web\u5ba2\u6237\u7aef\u3002<\/p>\n
Node.js\u53ef\u4ee5\u4f7f\u7528\u4e32\u53e3\u6a21\u5757\u6765\u8bbf\u95ee\u4e32\u53e3\u8bbe\u5907\uff0c\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u8bfb\u53d6\u4e32\u53e3\u6570\u636e\u3002\u8981\u4f7f\u7528\u8be5\u6a21\u5757\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\uff1a<\/p>\n
$ npm install serialport <\/code><\/pre>\n \u4e00\u65e6\u5b89\u88c5\u5b8c\u6210\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528\u8be5\u6a21\u5757\u6765\u8bbf\u95ee\u4e32\u53e3\u8bbe\u5907\u4e86\u3002\u9996\u5148\uff0c\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u6253\u5f00\u4e00\u4e2a\u4e32\u53e3\u8bbe\u5907\uff1a<\/p>\n
const SerialPort = require('serialport'); const port = new SerialPort('\/dev\/ttyUSB0', {<\/p>
baudRate: 9600<\/p>
});<\/p><\/code><\/pre>\n
\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u6253\u5f00\u4e00\u4e2a\u540d\u4e3a\/dev\/ttyUSB0\u7684\u4e32\u53e3\u8bbe\u5907\uff0c\u5e76\u8bbe\u7f6e\u6ce2\u7279\u7387\u4e3a9600\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u4ece\u4e32\u53e3\u8bbe\u5907\u4e2d\u8bfb\u53d6\u6570\u636e\uff1a<\/p>\n
port.on('data', (data) => { \/\/ do something with the data<\/p>
});<\/p><\/code><\/pre>\n
\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u76d1\u542c\u4e32\u53e3\u8bbe\u5907\u7684\u6570\u636e\uff0c\u5e76\u5728\u6536\u5230\u6570\u636e\u65f6\u6267\u884c\u56de\u8c03\u51fd\u6570\u3002\u56de\u8c03\u51fd\u6570\u4e2d\u7684data\u53c2\u6570\u5c31\u662f\u4ece\u4e32\u53e3\u8bbe\u5907\u4e2d\u8bfb\u53d6\u5230\u7684\u6570\u636e\u3002<\/p>\n
\u5c06\u6570\u636e\u4f20\u9012\u5230Web\u5ba2\u6237\u7aef<\/h2>\n
\u4e00\u65e6\u4ece\u4e32\u53e3\u8bbe\u5907\u4e2d\u8bfb\u53d6\u5230\u4e86\u6570\u636e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528Node.js\u7684WebSocket\u6a21\u5757\u5c06\u6570\u636e\u4f20\u9012\u5230Web\u5ba2\u6237\u7aef\u4e86\u3002\u9996\u5148\uff0c\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u5b89\u88c5WebSocket\u6a21\u5757\uff1a<\/p>\n
$ npm install ws <\/code><\/pre>\n \u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u521b\u5efa\u4e00\u4e2aWebSocket\u670d\u52a1\u5668\uff1a<\/p>\n
const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 });<\/p><\/code><\/pre>\n
\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u521b\u5efa\u4e00\u4e2a\u76d1\u542c\u57288080\u7aef\u53e3\u7684WebSocket\u670d\u52a1\u5668\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u5411WebSocket\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e\uff1a<\/p>\n
\/\/ Send data to all connected clients wss.clients.forEach(function each(client) {<\/p>
if (client.readyState === WebSocket.OPEN) {<\/p>
client.send(data);<\/p>
}<\/p>
});<\/p><\/code><\/pre>\n
\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u5411\u6240\u6709\u8fde\u63a5\u7684WebSocket\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u5c06\u4ece\u4e32\u53e3\u8bbe\u5907\u4e2d\u8bfb\u53d6\u5230\u7684\u6570\u636e\u4f20\u9012\u5230WebSocket\u5ba2\u6237\u7aef\uff1a<\/p>\n
port.on('data', (data) => { wss.clients.forEach(function each(client) {<\/p>
if (client.readyState === WebSocket.OPEN) {<\/p>
client.send(data);<\/p>
}<\/p>
});<\/p>
});<\/p><\/code><\/pre>\n
\u603b\u7ed3<\/h2>\n
\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Node.js\u53d6\u4e32\u53e3\uff0c\u5e76\u5c06\u6570\u636e\u4f20\u9012\u5230Web\u5ba2\u6237\u7aef\u3002<\/strong>\u9996\u5148\uff0c\u4f7f\u7528Node.js\u7684\u4e32\u53e3\u6a21\u5757\u4ece\u4e32\u53e3\u8bbe\u5907\u4e2d\u8bfb\u53d6\u6570\u636e\u3002\u63a5\u7740\uff0c\u4f7f\u7528Node.js\u7684WebSocket\u6a21\u5757\u5c06\u6570\u636e\u53d1\u9001\u5230Web\u5ba2\u6237\u7aef\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Node.js\u7684\u4e32\u53e3\u6a21\u5757\u548cWebSocket\u6a21\u5757\uff0c\u5c06\u4ece\u4e32\u53e3\u8bbe\u5907\u4e2d\u8bfb\u53d6\u5230\u7684\u6570\u636e\u4f20\u9012\u5230Web\u5ba2\u6237\u7aef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Node.js\u662f\u4e00\u4e2a\u57fa\u4e8eJavaScript\u7684\u5f00\u6e90\u670d\u52a1\u5668\u73af\u5883\uff0c\u5b83\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u4f7f\u7528JavaScript\u7f16\u5199\u670d\u52a1\u5668\u7aef\u5e94\u7528\u7a0b\u5e8f\u3002Node.js\u53ef\u4ee5\u63d0\u4f9b\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u5f0f\u6765\u4ece\u4e32<\/p>\n","protected":false},"author":1,"featured_media":4132,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[36],"tags":[69],"class_list":["post-38844","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-cktx"],"_links":{"self":[{"href":"\/\/www.dealsbon.com\/wp-json\/wp\/v2\/posts\/38844"}],"collection":[{"href":"\/\/www.dealsbon.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"\/\/www.dealsbon.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/\/www.dealsbon.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/\/www.dealsbon.com\/wp-json\/wp\/v2\/comments?post=38844"}],"version-history":[{"count":0,"href":"\/\/www.dealsbon.com\/wp-json\/wp\/v2\/posts\/38844\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"\/\/www.dealsbon.com\/wp-json\/wp\/v2\/media\/4132"}],"wp:attachment":[{"href":"\/\/www.dealsbon.com\/wp-json\/wp\/v2\/media?parent=38844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/\/www.dealsbon.com\/wp-json\/wp\/v2\/categories?post=38844"},{"taxonomy":"post_tag","embeddable":true,"href":"\/\/www.dealsbon.com\/wp-json\/wp\/v2\/tags?post=38844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}