-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5d67db
commit 227765b
Showing
9 changed files
with
2,657 additions
and
1,840 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* 网络请求,测试时请使用node命令启动服务端,在根目录下:node server.js | ||
*/ | ||
import rxwx from '../../utils/RxWX.js' | ||
|
||
/** | ||
* http 请求 | ||
*/ | ||
let handlerA = (res) => console.log('handler a:', res) | ||
let handlerB = (res) => console.log('handler b:', res) | ||
// 调用小程序API | ||
let url = 'http://localhost:3456' | ||
wx.request({ | ||
url, | ||
success(res) { | ||
// 逻辑与请求的紧耦合 | ||
handlerA(res) | ||
handlerB(res) | ||
} | ||
}) | ||
|
||
// 调用RxWX | ||
let req = rxwx.request({ | ||
url | ||
}) | ||
// 轻轻松松将业务逻辑与请求分离 | ||
req.subscribe(handlerA) | ||
req.subscribe(handlerB) | ||
|
||
/* | ||
* 建立websocket连接 | ||
*/ | ||
url = 'ws://localhost:34567' | ||
// 调用微信小程序API | ||
let ws = wx.connectSocket({ | ||
url | ||
}) | ||
ws.onOpen(() => { | ||
ws.send({ data: new Date }) | ||
ws.onMessage(msg => console.log(msg.data)) | ||
ws.close() | ||
ws.onClose(msg => console.log('Websocket closed.')) | ||
}) | ||
// 调用RxWX | ||
rxwx.connectSocket({ | ||
url | ||
}) | ||
.subscribe(ws => { | ||
ws.onOpen(() => { | ||
ws.send({ data: new Date }) | ||
ws.onMessage(msg => console.log(msg.data)) | ||
ws.close() | ||
ws.onClose(msg => console.log('Websocket closed.')) | ||
}) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.