{"id":3681,"date":"2024-11-10T08:02:12","date_gmt":"2024-11-10T00:02:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3681\/"},"modified":"2024-11-10T08:02:12","modified_gmt":"2024-11-10T00:02:12","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-javascript-%e4%b8%ad%e7%9a%84-try-catch-%e6%97%a0%e6%b3%95%e6%8d%95%e8%8e%b7-websocket-%e8%bf%9e%e6%8e%a5%e5%a4%b1%e8%b4%a5%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3681\/","title":{"rendered":"\u4e3a\u4ec0\u4e48 JavaScript \u4e2d\u7684 try catch \u65e0\u6cd5\u6355\u83b7 WebSocket \u8fde\u63a5\u5931\u8d25\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173111844320440.jpg\" class=\"aligncenter\" title=\"\u4e3a\u4ec0\u4e48 JavaScript \u4e2d\u7684 try catch \u65e0\u6cd5\u6355\u83b7 WebSocket \u8fde\u63a5\u5931\u8d25\u5f02\u5e38\uff1f\u63d2\u56fe\" alt=\"\u4e3a\u4ec0\u4e48 JavaScript \u4e2d\u7684 try catch \u65e0\u6cd5\u6355\u83b7 WebSocket \u8fde\u63a5\u5931\u8d25\u5f02\u5e38\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>javascript \u4e2d try catch \u65e0\u6cd5\u6355\u83b7 websocket \u8fde\u63a5\u5931\u8d25\u5f02\u5e38<\/strong><\/p>\n<p>javascript \u4e2d\u7684 try catch \u5757\u53ef\u4ee5\u6355\u83b7\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u4f46\u5b83\u65e0\u6cd5\u6355\u83b7 websocket \u8fde\u63a5\u5931\u8d25\u7684\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a websocket \u8fde\u63a5\u5931\u8d25\u662f\u4e00\u4e2a\u5185\u90e8\u5904\u7406\u4e8b\u4ef6\uff0c\u5e76\u672a\u660e\u786e\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u9690\u85cf\u63a7\u5236\u53f0\u4e2d\u7684 websocket \u8fde\u63a5\u5931\u8d25\u9519\u8bef\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li> <strong>\u5224\u65ad\u8fde\u63a5\u72b6\u6001\uff1a<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528 socket.open, socket.connecting, socket.closing \u7b49\u5c5e\u6027\u5224\u65ad websocket \u7684\u8fde\u63a5\u72b6\u6001\u3002\u5982\u679c\u8fde\u63a5\u72b6\u6001\u4e0d\u662f socket.open\uff0c\u5219\u8bf4\u660e\u8fde\u63a5\u5931\u8d25\u3002<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7ea7\u522b\uff1a<\/strong><br \/>\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e2d\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7ea7\u522b\uff0c\u5c06\u8fde\u63a5\u5931\u8d25\u9519\u8bef\u4fe1\u606f\u8fc7\u6ee4\u6389\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>console.log.level = 'info'; \/\/ \u4ec5\u663e\u793a info \u7ea7\u522b\u53ca\u4ee5\u4e0a\u7684\u65e5\u5fd7\u4fe1\u606f<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u4ec5\u9002\u7528\u4e8e\u9690\u85cf\u63a7\u5236\u53f0\u4e2d\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u4e0d\u80fd\u963b\u6b62\u8fde\u63a5\u5931\u8d25\u4e8b\u4ef6\u7684\u53d1\u751f\u3002\u5982\u679c\u4f60\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5904\u7406 websocket \u8fde\u63a5\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u8bf7\u4f7f\u7528 socket.onerror \u4e8b\u4ef6\u76d1\u542c\u5668\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f JavaScript \u4e2d\u7684 try catch \u65e0\u6cd5\u6355\u83b7 WebSocket \u8fde\u63a5\u5931\u8d25\u5f02\u5e38\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u4e2d try catch \u65e0\u6cd5\u6355\u83b7 websocket \u8fde\u63a5\u5931\u8d25\u5f02\u5e38 javascript \u4e2d\u7684 try catch \u5757\u53ef\u4ee5\u6355\u83b7\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u4f46\u5b83\u65e0\u6cd5\u6355\u83b7 websocket \u8fde\u63a5\u5931\u8d25\u7684\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a websocket \u8fde\u63a5\u5931\u8d25\u662f\u4e00\u4e2a\u5185\u90e8\u5904\u7406\u4e8b\u4ef6\uff0c\u5e76\u672a\u660e\u786e\u629b\u51fa\u5f02\u5e38\u3002 \u89e3\u51b3\u65b9\u6cd5 \u4e3a\u4e86\u9690\u85cf\u63a7\u5236\u53f0\u4e2d\u7684 websocket \u8fde\u63a5\u5931\u8d25\u9519\u8bef\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5224\u65ad\u8fde\u63a5\u72b6\u6001\uff1a\u53ef\u4ee5\u4f7f\u7528 socket.open, socket.connecting, socket.closing \u7b49\u5c5e\u6027\u5224\u65ad websocket \u7684\u8fde\u63a5\u72b6\u6001\u3002\u5982\u679c\u8fde\u63a5\u72b6\u6001\u4e0d\u662f socket.open\uff0c\u5219\u8bf4\u660e\u8fde\u63a5\u5931\u8d25\u3002 \u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7ea7\u522b\uff1a\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e2d\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7ea7\u522b\uff0c\u5c06\u8fde\u63a5\u5931\u8d25\u9519\u8bef\u4fe1\u606f\u8fc7\u6ee4\u6389\u3002\u4f8b\u5982\uff1a console.log.level = &#8216;info&#8217;; \/\/ \u4ec5\u663e\u793a info \u7ea7\u522b\u53ca\u4ee5\u4e0a\u7684\u65e5\u5fd7\u4fe1\u606f \u767b\u5f55\u540e\u590d\u5236 \u63d0\u793a\uff1a \u8bf7\u6ce8\u610f\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u4ec5\u9002\u7528\u4e8e\u9690\u85cf\u63a7\u5236\u53f0\u4e2d\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u4e0d\u80fd\u963b\u6b62\u8fde\u63a5\u5931\u8d25\u4e8b\u4ef6\u7684\u53d1\u751f\u3002\u5982\u679c\u4f60\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5904\u7406 websocket \u8fde\u63a5\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u8bf7\u4f7f\u7528 socket.onerror \u4e8b\u4ef6\u76d1\u542c\u5668\u3002 \u4ee5\u4e0a\u5c31\u662f JavaScript \u4e2d\u7684 try catch \u65e0\u6cd5\u6355\u83b7 WebSocket \u8fde\u63a5\u5931\u8d25\u5f02\u5e38\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3681","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3681"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3681\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}