W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
解釋:通過 WebSocket 連接發(fā)送數(shù)據(jù)。
Object object
屬性名 | 類型 | 必填 | 默認(rèn)值 | 說明 | ||
---|---|---|---|---|---|---|
data |
String/ArrayBuffer |
是 |
需要發(fā)送的內(nèi)容 |
|||
success |
Function |
否 |
接口調(diào)用成功的回調(diào)函數(shù) |
|||
fail |
Function |
否 |
接口調(diào)用失敗的回調(diào)函數(shù) |
|||
complete |
Function |
否 |
接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會執(zhí)行) |
<button type="primary" bindtap="socketTaskSend">socketTask.send</button>
Page({
onShow() {
const socketTask = new Promise((resolve, reject) => {
const socketHandler = swan.connectSocket({
url: 'wss://echo.websocket.org',
success: res => {
console.log('connectSocket success', res.socketTaskId);
},
fail: err => {
reject(err);
console.log('connectSocket fail', err);
}
});
socketHandler.onOpen(function () {
resolve(socketHandler);
});
});
this.socketTask = socketTask;
},
socketTaskSend() {
this.socketTask.then(socketHandler => {
socketHandler.send({
data: 'baidu',
success: res => {
swan.showToast({
title: '發(fā)送數(shù)據(jù)成功',
icon: 'none'
});
console.log('WebSocket發(fā)送數(shù)據(jù)成功', res);
},
fail: err => {
swan.showModal({
title: '發(fā)送數(shù)據(jù)失敗',
content: JSON.stringify(err)
});
console.log('WebSocket發(fā)送數(shù)據(jù)失敗', err);
}
});
});
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: