Skip to content

Commit

Permalink
update network request
Browse files Browse the repository at this point in the history
  • Loading branch information
yalishizhude committed Feb 9, 2018
1 parent a5d67db commit 227765b
Show file tree
Hide file tree
Showing 9 changed files with 2,657 additions and 1,840 deletions.
2 changes: 1 addition & 1 deletion RxWX.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions example/pages/demo/network.js
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.'))
})
})

1 change: 1 addition & 0 deletions example/pages/index/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import a from '../demo/sync_async.js'
import b from '../demo/nest.js'
import c from '../demo/combine.js'
import d from '../demo/network.js'

//index.js
//获取应用实例
Expand Down
2 changes: 1 addition & 1 deletion example/utils/RxWX.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 227765b

Please sign in to comment.