简体   繁体   English

如何使用 websockets 在我的 Azure 应用服务 Web 应用和本地控制台应用之间建立连接?

[英]How do I establish a connection between my Azure App Service Web App and local console app using websockets?

I have a REST API Web App deployed in Azure as an App Service, which I can successfully make requests to from a console application on my device. I have a REST API Web App deployed in Azure as an App Service, which I can successfully make requests to from a console application on my device. My goal is to transition from passing JSON's back and forth via HttpClient on my console side to establishing a connection between the two via web sockets.我的目标是从通过控制台端的 HttpClient 来回传递 JSON 过渡到通过 web sockets 在两者之间建立连接。

Currently, I have the following function that gets called on startup in my App Service.目前,我有以下 function 在我的应用服务启动时被调用。

public static void StartListening()
{
    try
    {
        TcpListener listener = new TcpListener(IPAddress.Any, 80);

        listener.Start();

        using (TcpClient client = listener.AcceptTcpClient())
        {
            using (Stream stream = client.GetStream())
            {
                byte[] requestBuffer = new byte[100];
                int size = stream.Read(requestBuffer, 0, requestBuffer.Length);
                string msg = Encoding.ASCII.GetString(requestBuffer, 0, size);
            }
        }
    } catch (Exception ex) { /* handle exception here */ }
}

And in my console application:在我的控制台应用程序中:

static void StartConnection(string msg)
{
    try
    {
        Console.WriteLine("Accessing tcp");
        byte[] buffer = Encoding.ASCII.GetBytes(msg);

        using (TcpClient client = new TcpClient("hostname here", 80))
        {
            using (Stream stream = client.GetStream())
            {
                stream.Write(buffer, 0, buffer.Length);
            }
        }
    } catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); }
}

For reference, I used the format found here as my guide.作为参考, 我使用此处找到的格式作为我的指南。

I've made sure that web sockets are set to "on" in my App Service's configuration.我已确保在我的应用服务配置中将 web sockets 设置为“on”。 Other than that, I'm not aware of any changes I need to make thus far.除此之外,到目前为止,我不知道我需要进行任何更改。

My issue comes when actually trying to call the function on the server side.当实际尝试在服务器端调用 function 时,我的问题就出现了。 With IpAddress.Any in my listener parameter, I get a 10013 error (Permission denied).在我的侦听器参数中使用 IpAddress.Any 时,我收到 10013 错误(权限被拒绝)。 Changing the port will sometimes change the error to 10048 (Already in use).更改端口有时会将错误更改为 10048(已在使用中)。 When I substitute IpAddress.Any with the Inbound IP, my error changes to 10049 (Cannot assign requested address. The requested address is not valid in its context).当我用入站 IP 替换 IpAddress.Any 时,我的错误更改为 10049(无法分配请求的地址。请求的地址在其上下文中无效)。 Error code references here 错误代码参考这里

If I understand correctly, Azure App Service's can only accept inbound traffic on ports 80 and 443, neither of which work in my case.如果我理解正确,Azure App Service 只能接受端口 80 和 443 上的入站流量,这两种情况都不适用于我的情况。 I don't have much experience working with these technologies, so any insight is welcome.我没有太多使用这些技术的经验,所以欢迎任何见解。

Also, I have only been able to debug until the listener.Start() line in my code, since my current execution stops there.此外,我只能在代码中的 listener.Start() 行之前进行调试,因为我当前的执行在那里停止。 Because of this, if there's anything obvious that jumps out in the code that follows that, please feel free to point it out.因此,如果在后面的代码中有任何明显的跳出,请随时指出。

You'd better use a tool like ngrok which will act like a proxy and send data to your local dev station.您最好使用像 ngrok 这样的工具,它可以充当代理并将数据发送到本地开发站。

eg:例如:

send data to ngrok / it will route to your localhost将数据发送到 ngrok / 它将路由到您的本地主机

Please use something in the middle, when using web sockets recommended way would be to use SignalR.请在中间使用一些东西,当使用 web sockets 推荐的方法是使用 SignalR。 Azure SignalR is a platform service and using web sockets or REST or other protocols you can do this, it will be scalable and not bring down your App Service as well by not using too many requests. Azure SignalR is a platform service and using web sockets or REST or other protocols you can do this, it will be scalable and not bring down your App Service as well by not using too many requests.

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

相关问题 如何从我的 SQL Azure 应用服务 Web 应用写入日志文件? - How do I write a log file from my SQL Azure App Service Web App? 使用Websockets的Web App虚拟控制台 - Virtual Console for Web App Using Websockets SignalR Core 未在 Azure 应用服务中使用 Websocket - SignalR Core not using Websockets in Azure App Service 如何解决我的 azure web 应用程序机器人上的 401? - How do I resolve a 401 on my azure web app bot? 当我使用生产(基础应用服务)插槽时,为什么 Azure 应用服务不能可靠地读取我的连接字符串 - Why is Azure app service not reliably reading my connection strings when I am using the production (base app service) slot 如何使用 Azure 应用服务本地缓存? - How to use Azure App Service Local Cache? Azure Web应用程序服务使用混合连接管理器使用HttpClient调用本地WEB API - Azure web app service to call onpremise WEB API using HttpClient using hybrid connection manager 如何在Web App Service中使用“Azure文件存储”? - How can I use “Azure File Storage” with Web App Service? 如何使用 AzCopy 使用 cmd 在 Azure Web 应用服务中工作 - How to use AzCopy to work in Azure web app service using cmd 使用Web API在网站和本地控制台应用程序之间进行通信 - Use Web API to communicate between Website and local Console App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM