Cyclejs Read/write websocket driver?

[亡魂溺海] 提交于 2019-12-12 02:24:21

问题


I'm new to cyclejs and I'm looking for websocket support and I don't see any (apart from the read only websocket driver from the docs and some 0.1.2 node side npm package).

Am I supposed to create my own driver or am I missing something?

Thanks in advance


回答1:


Does this page help you?

https://cycle.js.org/drivers.html

Specifically the example code mentioned:

function WSDriver(/* no sinks */) {
  return xs.create({
    start: listener => {
       this.connection = new WebSocket('ws://localhost:4000');
       connection.onerror = (err) => {
          listener.error(err)
       }
       connection.onmessage = (msg) => {
         listener.next(msg)
       }
    },
    stop: () => {
      this.connection.close();
    },
 });
}

If you add a sink this should be a write and read driver. From their documentation:

Most drivers, like the DOM Driver, take sinks (to describe a write) and return sources (to catch reads). However, we might have valid cases for write-only drivers and read-only drivers.

For instance, the one-liner log driver we just saw above is a write-only driver. Notice how it is a function that does not return any stream, it simply consumes the sink msg$ it receives.

Other drivers only create source streams that emit events to the main(), but don’t take in any sink from main(). An example of such would be a read-only Web Socket driver, drafted below:



来源:https://stackoverflow.com/questions/42871626/cyclejs-read-write-websocket-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!