简体   繁体   English

C#可以使用Socket(Not WebSocket)类连接到WebSocket吗

[英]Can C# connect to a WebSocket using Socket(Not WebSocket) class

I have a c# app that communicates over socket. 我有一个通过套接字进行通信的c#应用。 I want to code a web app that can connect to c# app over socket. 我想编写一个可以通过套接字连接到c#应用程序的Web应用程序。 As you know c# has different classes for Websocket and Socket . 如您所知,c#具有WebsocketSocket不同类。

Questions : 问题:

1-) If I use WebSocket for JavaScript , can it connect to existing Socket structure on c# or is it required to re-code it to WebSocket ? 1-)如果我将WebSocket用于JavaScript ,它可以连接到c#上的现有Socket结构,还是需要将其重新编码为WebSocket

2-) is there any other performance efficent way to do ? 2-)还有其他有效的方法吗?

A WebSocket connection uses a socket, but with a predefined way of framing the data, while if you use a socket directly, you can define your own way of framing the data, however the other end must know how the data is framed in order to understand it. WebSocket连接使用套接字,但是具有预定义的数据构架方式,而如果直接使用套接字,则可以定义自己的数据构架方式,但是另一端必须知道数据的结构以便理解。

Answering your question: In Javascript you can only connect to sockets that are running as WebSocket, since the WebSocket Javascript client will try to communicate in the "WebSocket" way and won't understand anything different. 回答您的问题:在Javascript中,您只能连接到以WebSocket运行的套接字,因为WebSocket Javascript客户端将尝试以“ WebSocket”方式进行通信,并且不会有所不同。

However, if you really need Javascript to access a regular socket, you can try to use an intermediary proxy like WebSockify , but you cannot connect directly. 但是,如果您确实需要Javascript来访问常规套接字,则可以尝试使用WebSockify之类的中介代理,但不能直接连接。

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

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