{"id":11451,"date":"2021-05-04T07:44:09","date_gmt":"2021-05-03T23:44:09","guid":{"rendered":"https:\/\/fgchen.com\/wpedu\/?post_type=lp_course&#038;p=16706"},"modified":"2023-03-02T20:09:28","modified_gmt":"2023-03-02T12:09:28","slug":"%e4%bd%bf%e7%94%a8node-js%e5%ae%89%e8%a3%9dwebsocket%e5%81%9a%e5%87%ba%e5%a4%9a%e4%ba%ba%e8%81%8a%e5%a4%a9%e5%ae%a4","status":"publish","type":"lp_course","link":"https:\/\/fgchen.com\/wpedu\/course\/%e4%bd%bf%e7%94%a8node-js%e5%ae%89%e8%a3%9dwebsocket%e5%81%9a%e5%87%ba%e5%a4%9a%e4%ba%ba%e8%81%8a%e5%a4%a9%e5%ae%a4\/","title":{"rendered":"\u4f7f\u7528node.js\u5b89\u88ddwebsocket\u505a\u51fa\u591a\u4eba\u804a\u5929\u5ba4"},"content":{"rendered":"1. \u5b89\u88ddnode.js\n\n\u5b98\u7db2: https:\/\/nodejs.org\/en\/\n\n2.\u958b\u555fcmd\u8f38\u5165\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">npm install express\nnpm install ws\nnpm install mysql<\/pre>\n&nbsp;\n\n3.\u5275\u5efaserver.js\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/import express \u548c ws \u5957\u4ef6\nconst express = require('express')\nconst SocketServer = require('ws').Server\n\n\/\/\u6307\u5b9a\u958b\u555f\u7684 port\nconst PORT = 3000\n\n\/\/\u5275\u5efa express \u7684\u7269\u4ef6\uff0c\u4e26\u7d81\u5b9a\u53ca\u76e3\u807d 3000 port \uff0c\u4e14\u8a2d\u5b9a\u958b\u555f\u5f8c\u5728 console \u4e2d\u63d0\u793a\nconst server = express()\n    .listen(PORT, () =&gt; console.log(`Listening on ${PORT}`))\n\n\/\/\u5c07 express \u4ea4\u7d66 SocketServer \u958b\u555f WebSocket \u7684\u670d\u52d9\nconst wss = new SocketServer({ server })\n\n\/\/\u7576 WebSocket \u5f9e\u5916\u90e8\u9023\u7d50\u6642\u57f7\u884c\nwss.on('connection', ws =&gt; {\n    console.log('Client connected')\n\n    ws.on('message', data =&gt; {\n        \/\/\u53d6\u5f97\u6240\u6709\u9023\u63a5\u4e2d\u7684 client\n        let clients = wss.clients\n\n        \/\/\u505a\u8ff4\u5708\uff0c\u767c\u9001\u8a0a\u606f\u81f3\u6bcf\u500b client\n        clients.forEach(client =&gt; {\n            client.send(data)\n        })\n    })\n\n    ws.on('close', () =&gt; {\n        console.log('Close connected')\n    })\n})\n<\/pre>\n&nbsp;\n\n4.\u5275\u5efa\u9023\u7dda\u7db2\u9801\u6a94(text.html)\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;html&gt;\n&lt;body&gt;\n&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.5.1.min.js\"&gt;&lt;\/script&gt;\n&lt;script&gt;\n$(document).ready(function(){\n  $('#txtInput').keypress(function(e) {\n    var key = window.event ? e.keyCode : e.which;\n    if (key == 13)\n      $('#btnSend').click();\n  });\n  $(\"#btnSend\").on(\"click\",function(){\n    let txt = $(\"#txtInput\").val();\n    $(\"#txtInput\").val(\"\");\n    ws.send(txt);\n  })\n})\n\/\/\u4f7f\u7528 WebSocket \u7684\u7db2\u5740\u5411 Server \u958b\u555f\u9023\u7d50\nvar ws = new WebSocket('ws:\/\/localhost:3000')\n\nws.onopen = () =&gt; {\n  console.log('open connection')\n}\n\nws.onclose = () =&gt; {\n  console.log('close connection');\n}\n\n\/\/\u63a5\u6536 Server \u767c\u9001\u7684\u8a0a\u606f\nws.onmessage = event =&gt; {\n  console.log(event);\n  if(!$(\"#txtShow\").val())\n    $(\"#txtShow\").val(event.data);\n  else\n    $(\"#txtShow\").val($(\"#txtShow\").val()+\"n\"+event.data);\n  }\n&lt;\/script&gt;\n&lt;textarea id=\"txtShow\" style=\"height:60vmin;width:80vmin\" disabled&gt;&lt;\/textarea&gt;&lt;\/br&gt;\n&lt;input id=\"txtInput\" style=\"width:80vmin\" type=\"text\"&gt;&lt;input type=\"button\" id=\"btnSend\" value=\"\u9001\u51fa\"&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n&nbsp;\n\n5.\u958b\u555fcmd\u5230\u8cc7\u6599\u593e\u76ee\u9304\u8f38\u5165\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Node server.js<\/pre>\n<img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-16707\" src=\"https:\/\/fgchen.com\/wpedu\/wp-content\/uploads\/2020\/12\/1-300x106.png\" alt=\"\" width=\"583\" height=\"206\" \/>\n\n6.\u6253\u958b\u7db2\u9801\u6a94 html\uff0c\u958b\u5169\u4efd(\u4e00\u5de6\u4e00\u53f3)\n\n<img decoding=\"async\" class=\"alignnone wp-image-16708\" src=\"https:\/\/fgchen.com\/wpedu\/wp-content\/uploads\/2020\/12\/2-300x136.png\" alt=\"\" width=\"587\" height=\"266\" \/>\n\n7.\u6e2c\u8a66\u6a94\u6848\u5728textbox\u8f38\u5165\u8cc7\u6599(123)\n\n<img decoding=\"async\" class=\"alignnone wp-image-16709\" src=\"https:\/\/fgchen.com\/wpedu\/wp-content\/uploads\/2020\/12\/3-300x137.png\" alt=\"\" width=\"589\" height=\"269\" \/>\n\n8.\u5728\u4e0b\u65b9console\u8f38\u5165 ws.send(&#8220;hi&#8221;)\n\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16710\" src=\"https:\/\/fgchen.com\/wpedu\/wp-content\/uploads\/2020\/12\/4-300x76.png\" alt=\"\" width=\"580\" height=\"147\" \/>\n\n9.\u756b\u9762\u56de\u61c9\n\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16711\" src=\"https:\/\/fgchen.com\/wpedu\/wp-content\/uploads\/2020\/12\/5-300x217.png\" alt=\"\" width=\"536\" height=\"388\" \/>\t","protected":false},"excerpt":{"rendered":"<p>1. \u5b89\u88ddnode.js \u5b98\u7db2: https:\/\/nodejs.org\/en\/  &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","course_category":[],"course_tag":[],"class_list":["post-11451","lp_course","type-lp_course","status-publish","hentry","course"],"_links":{"self":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/lp_course\/11451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/lp_course"}],"about":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/types\/lp_course"}],"author":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/comments?post=11451"}],"version-history":[{"count":1,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/lp_course\/11451\/revisions"}],"predecessor-version":[{"id":13579,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/lp_course\/11451\/revisions\/13579"}],"wp:attachment":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/media?parent=11451"}],"wp:term":[{"taxonomy":"course_category","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/course_category?post=11451"},{"taxonomy":"course_tag","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/course_tag?post=11451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}