前端向后端请求数据从上古时期就不是什么新鲜事,后端向前端推送数据自从有了 socket.io 这种神器以后也没什么大不了的了,那为啥后端为什么不可以向前端那用个类似 Ajax 的东西请求数据?于是我就造了这么个东西 IORequest https://github.com/bramblex/io-request ,不仅能用 socket.io 像 Ajax 一样从前端向后端发送请求,同时也可以从后端向前端发送请求。
假设我们现在要做一个简单 web 版本的猜拳对战游戏,服务器端的游戏逻辑只需要这样写
class Game { constructor (left, right) { co(function *() { const home_id = next_home_id++ left.emit(`msg`, `游戏开始,游戏房间号[${home_id}],等待你出拳`) right.emit(`msg`, `游戏开始,游戏房间号[${home_id}],等待你出拳`) // 这里就是向前端请求 choice 方法,并且等待前端的返回 const [l, r] = yield [ left.ioRequest({method: 'choice'}), right.ioRequest({method: 'choice'}) ] // 后端接受到前端的返回后继续游戏的进程 const _choices = [null, '布', '剪刀', '石头'] left.emit('msg', `你出的是[${_choices[l]}], 对方出的是[${_choices[r]}]`) right.emit('msg', `你出的是[${_choices[r]}], 对方出的是[${_choices[l]}]`) // 比较并且得出结果 if (Game.compare(l, r)) { left.emit('msg', '你赢了') right.emit('msg', '你输了') } else { right.emit('msg', '你赢了') left.emit('msg', '你输了') } // 游戏结束,关闭链接 right.disconnect() left.disconnect() }) } }
而前端逻辑更简单了
// 前端在这里只需要处理后端发送的 choice 请求,并且处理完后通过 response 返回请求的数据 ioReqClient.handle('choice', ({response}) => { const choices = document.getElementById('choices') choices.style.display = 'block' const shi = document.getElementById('shi') const jian = document.getElementById('jian') const bu = document.getElementById('bu') // 当按钮被点击的时候 shi.Onclick= () => { response(3) // 在这里给后端返回 choices.style.display = 'none' msgLog('我出的是[石头]') } // 同上 jian.Onclick= () => { response(2) choices.style.display = 'none' msgLog('我出的是[剪刀]') } // 同上 bu.Onclick= () => { response(1) choices.style.display = 'none' msgLog('我出的是[布]') } })
这里是上面那个例子的完整实现。 https://github.com/bramblex/io-request/tree/master/example
1 afpro 2016-12-13 19:40:11 +08:00 你想要的是 Wt |
![]() | 2 aristotle9 2016-12-13 23:29:42 +08:00 Reverse Ajax, Part 1: Introduction to Comet http://www.ibm.com/developerworks/library/wa-reverseajax1/ |
![]() | 3 latelx 2016-12-15 02:38:34 +08:00 思路不错 |