我的websocket类Wws
使用onmessage
侦听所有传入的消息。
对于使用Wws
的程序,我的websocket模块,我希望他们能够"听"通过iterable
s。
例如,
// listen for new blog messages let itr = ws.listen( 'blog', 'new' ); // display each new blog as we receive it while( true ) { let blog = itr.next().value; dispBlog( blog ); }
任何数量的iterable
都可能挂在事件监听器上。
您如何在generator
模块中编写ws
?
ws = function() { conn = new Websocket(...); // could have global `onmessage` that passes through the messages conn.onmessage = function(msg) { // can look at listeners and know there are iterators // but how to talk to them? } listeners = new Map(); // generator for listening to incoming messages listener = function*( service, action ) { // can add this service.action to listeners, but what? // does this guy create his own `onmessage`? // if so, how does he get the `yield` to the outer function? }
如何将listener
与callback
转换为iterator
?