简体   繁体   English

Hapi.js Websocket协议通信是否需要使用hapi.js来使客户端连接到Websocket?

[英]Does the Hapi.js websocket protocol communication need to use hapi.js for the client to connect to the websocket?

I am building an API using Hapi. 我正在使用Hapi构建API。 I need WebSocket functionality and it seems that Nes is the most popular to be used with Hapi. 我需要WebSocket功能,看来Nes是与Hapi一起使用的最流行的工具。 This is fine since Nes makes things quite easy, for example, a test route might look as so... 这样做很好,因为Nes使事情变得非常容易,例如,一条测试路线可能看起来像这样...

// Register Nes.
await server.register(Nes);
...
...
// WebSocket route.
server.route({
  method: 'GET',
  path: '/h',
  config: {
    id: 'hello',
    handler: (request, h) => {
      return 'world!';
    }
  }
});

This is great, however, the documentation shows that the only way to make a request to this route using WebSockets is by using Nes on the client as well... 很好,但是文档显示,使用WebSockets对此路由进行请求的唯一方法是在客户端上也使用Nes。

const Nes = require('nes');

var client = new Nes.Client('ws://localhost');

const start = async () => {

  await client.connect();
  const payload = await client.request('hello');  // Can also request '/h'
  // payload -> 'world!'
};

start();

My issue is that the client does not use JavaScript. 我的问题是客户端不使用JavaScript。 The Nes library does not exist at all. Nes库根本不存在。 In that case, can I still use WebSockets to make a request to this route? 在那种情况下,我仍然可以使用WebSockets对此路由进行请求吗? There are no examples of this so I do not understand how I could do so. 没有任何示例,因此我不知道该怎么做。 If it is not possible, then I would like to know what my options are as even Socket.io does not exist in the framework I am using (Flutter). 如果不可能,那么我想知道我的选择是什么,因为我正在使用的框架(Flutter)中甚至不存在Socket.io。

Since WebSockets are just a protocol , any WebSocket client library for flutter should work. 因为WebSockets只是一个协议 ,所以任何用于Flutter的WebSocket客户端库都应该可以工作。 WebSockets are not tied directly to a specific language. WebSocket不直接与特定语言绑定。 Thus, you have to find and implement a WebSocket library for your framework. 因此,您必须为框架找到并实现WebSocket库。 I browsed their website and found a few potential candidates, though I'm sure it's not an exhaustive list. 我浏览了他们的网站,发现了一些潜在的候选人,尽管我确信这不是一个详尽的清单。

Here are some potential web socket packages from Flutter: 以下是Flutter提供的一些潜在的Web套接字软件包:

For information on integrating the package, check out this link here. 有关集成软件包的信息,请在此处查看此链接

As for developing web sockets with Hapi, you don't have to use NES . 至于发展与哈啤网络插座,你不必使用NES It may make the most sense to use a library, like Socket.io that is developed by the same publisher for both the client and server. 使用由同一发布者为客户端和服务器开发的库(如Socket.io)可能是最有意义的。 While using Socket.io with Hapi is beyond the scope of this answer, you may find this medium article helpful. 虽然将Socket.io与Hapi一起使用超出了此答案的范围,但您可能会发现这篇中篇文章很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM