🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# wx.closeSocket ### wx.closeSocket(Object object) 关闭 WeSocket 连接 #### 参数 ##### Object object 属性类型默认值是否必填说明支持版本codenumber1000(表示正常关闭连接)否一个数字值表示关闭连接的状态号,表示连接被关闭的原因。reasonstring否一个可读的字符串,表示连接被关闭的原因。这个字符串必须是不长于 123 字节的 UTF-8 文本(不是字符)。successfunction否接口调用成功的回调函数failfunction否接口调用失败的回调函数completefunction否接口调用结束的回调函数(调用成功、失败都会执行)#### 示例代码 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">wx</span><span class="pun">.</span><span class="pln">connectSocket</span><span class="pun">({</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> url</span><span class="pun">:</span><span class="pln"> </span><span class="str">'test.php'</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="com">//注意这里有时序问题,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="com">//如果 wx.connectSocket 还没回调 wx.onSocketOpen,而先调用 wx.closeSocket,那么就做不到关闭 WebSocket 的目的。</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="com">//必须在 WebSocket 打开期间调用 wx.closeSocket 才能关闭。</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">wx</span><span class="pun">.</span><span class="pln">onSocketOpen</span><span class="pun">(</span><span class="hljs-params"><span class="kwd">function</span></span><span class="pun">(</span><span class="hljs-params"></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> wx</span><span class="pun">.</span><span class="pln">closeSocket</span><span class="pun">()</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">wx</span><span class="pun">.</span><span class="pln">onSocketClose</span><span class="pun">(</span><span class="hljs-params"><span class="kwd">function</span></span><span class="pun">(</span><span class="hljs-params"><span class="pln">res</span></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'WebSocket 已关闭!'</span><span class="pun">)</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> </ol> ``` > 原文: <https://developers.weixin.qq.com/minigame/dev/api/network/websocket/wx.closeSocket.html>