问题描述
我是 cyclejs 的新手,我正在寻找 websocket 支持,但我没有看到任何支持(除了文档中的只读 websocket 驱动程序和一些 0.1.2 节点端 npm 包).
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?
提前致谢
推荐答案
此页面对您有帮助吗?
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:
大多数驱动程序,如 DOM 驱动程序,采用接收器(描述写入)并返回源(捕捉读取).但是,对于只写驱动程序和只读驱动程序,我们可能有有效的案例.
例如,我们刚刚在上面看到的单行日志驱动程序是一个只写驱动程序.请注意它是一个如何不返回任何流的函数,它只是使用接收到的接收器 msg$.
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.
其他驱动程序只创建向 main() 发出事件的源流,但不从 main() 接收任何接收器.一个这样的例子是一个只读的 Web Socket 驱动程序,起草如下:
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:
这篇关于Cyclejs 读/写 websocket 驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!