如果您不编写Web应用程序,并且您的客户端不是浏览器,那么Web套接字优于常规套接字的优势是什么?

时间:2021-09-06 04:59:40

There are some literature on the differences between WebSockets and sockets on *. What I'm trying to understand, is why would I pick web sockets over normal sockets, if I'm writing a normal client/server application, where the server is not necessarily a web server and the clients connecting to the server is a native C++ or a Java client.

有一些关于*上WebSockets和套接字之间差异的文献。我想要了解的是,为什么我会选择正常套接字上的Web套接字,如果我正在编写一个普通的客户端/服务器应用程序,其中服务器不一定是Web服务器,并且连接到服务器的客户端是本机的C ++或Java客户端。

There's a huge advantage to WebSockets if you have to deal with browsers, what if I don't have to support browsers, should I still consider using WebSockets?

如果你不得不处理浏览器,那么WebSockets有一个巨大的优势,如果我不需要支持浏览器,我还应该考虑使用WebSockets吗?

Edit:

More clarity to what I'm asking: Had WebSockets existed much earlier, would it be appropreate to replace https://en.wikipedia.org/wiki/Unix_domain_socket with WebSocket in the unix tools that use Unix domain socket for instance, or any other context where sockets are used in an IPC context. Another case would be if you were to write an IRC server, would you it be appropriate to use WebSocket instead of the ordinary TCP socket, is there any reason to stay away from WebSocket in this case?

更清楚我要问的是:如果WebSockets存在的时间要早​​得多,那么在使用Unix域套接字的unix工具中替换https://en.wikipedia.org/wiki/Unix_domain_socket并使用WebSocket是否合适呢?在IPC上下文中使用套接字的其他上下文。另一种情况是,如果您要编写IRC服务器,是否适合使用WebSocket而不是普通的TCP套接字,在这种情况下是否有理由远离WebSocket?

1 个解决方案

#1


3  

If you're comparing WebSockets to TCP, WebSockets have a number of advantages:

如果您将WebSockets与TCP进行比较,WebSockets有许多优点:

  • Unicode support
  • Datagram-oriented interactions
  • HTTP CONNECT-based firewall traversal
  • 基于HTTP CONNECT的防火墙遍历

However, this comparison is a bit of a false dichotomy. If you don't use WebSockets, you're not forced to start building directly on TCP. There are many other protocols out there (too many to count) with many great implementations.

然而,这种比较是一种错误的二分法。如果不使用WebSockets,则不必强制直接在TCP上构建。还有许多其他协议(太多无法计算),有很多很棒的实现。

Perhaps HTTP itself is well-suited to your application. Or maybe XMPP or Google Protocol Buffers or something else. It all depends on what your application needs to communicate.

也许HTTP本身非常适合您的应用程序。或者可能是XMPP或Google Protocol Buffers或其他东西。这一切都取决于您的应用程序需要进行通信。

Before you can select a protocol (or, rarely, design your own), you need to figure out what kind of communication your application needs to do. Then you can evaluate different protocols and decide which fit your problem well.

在您选择协议(或很少设计自己的协议)之前,您需要确定应用程序需要执行的通信类型。然后,您可以评估不同的协议,并确定哪个适合您的问题。

That said, the majority of development these days seems to focus around the web. Even if there's nothing inherently web-y about the application, many people choose HTTP as their protocol now. At this point, a ton of work has gone in to standardizing the use of HTTP as the solution for a wide range of problems, as well as building libraries to facilitate this use. Chances are, whatever you're building, HTTP will be a decent fit (again, I don't know what you're building, so this is just a guess).

也就是说,这些天的大部分发展似乎都集中在网络上。即使没有关于应用程序的固有web-y,很多人现在都选择HTTP作为他们的协议。在这一点上,大量的工作已经用于标准化HTTP的使用,作为解决各种问题的方法,以及构建库以促进这种使用。无论你正在构建什么,机会都适合(再次,我不知道你在构建什么,所以这只是猜测)。

#1


3  

If you're comparing WebSockets to TCP, WebSockets have a number of advantages:

如果您将WebSockets与TCP进行比较,WebSockets有许多优点:

  • Unicode support
  • Datagram-oriented interactions
  • HTTP CONNECT-based firewall traversal
  • 基于HTTP CONNECT的防火墙遍历

However, this comparison is a bit of a false dichotomy. If you don't use WebSockets, you're not forced to start building directly on TCP. There are many other protocols out there (too many to count) with many great implementations.

然而,这种比较是一种错误的二分法。如果不使用WebSockets,则不必强制直接在TCP上构建。还有许多其他协议(太多无法计算),有很多很棒的实现。

Perhaps HTTP itself is well-suited to your application. Or maybe XMPP or Google Protocol Buffers or something else. It all depends on what your application needs to communicate.

也许HTTP本身非常适合您的应用程序。或者可能是XMPP或Google Protocol Buffers或其他东西。这一切都取决于您的应用程序需要进行通信。

Before you can select a protocol (or, rarely, design your own), you need to figure out what kind of communication your application needs to do. Then you can evaluate different protocols and decide which fit your problem well.

在您选择协议(或很少设计自己的协议)之前,您需要确定应用程序需要执行的通信类型。然后,您可以评估不同的协议,并确定哪个适合您的问题。

That said, the majority of development these days seems to focus around the web. Even if there's nothing inherently web-y about the application, many people choose HTTP as their protocol now. At this point, a ton of work has gone in to standardizing the use of HTTP as the solution for a wide range of problems, as well as building libraries to facilitate this use. Chances are, whatever you're building, HTTP will be a decent fit (again, I don't know what you're building, so this is just a guess).

也就是说,这些天的大部分发展似乎都集中在网络上。即使没有关于应用程序的固有web-y,很多人现在都选择HTTP作为他们的协议。在这一点上,大量的工作已经用于标准化HTTP的使用,作为解决各种问题的方法,以及构建库以促进这种使用。无论你正在构建什么,机会都适合(再次,我不知道你在构建什么,所以这只是猜测)。