Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell WebSocket(客户端WebSocket)发送消息_Powershell - Fatal编程技术网

Powershell WebSocket(客户端WebSocket)发送消息

Powershell WebSocket(客户端WebSocket)发送消息,powershell,Powershell,环境:马科斯 我正在尝试使用System.Net.HttpListener创建菜单 是否有任何方法可以使用[System.Net.WebSocket.ClientWebSocket]$WebSocket使用Powershell向浏览器客户端发送信息?您可以这样做,但为了让web服务器向浏览器发送任何信息,浏览器必须启动请求,最常见的情况是使用JavaScript 首先,为服务器设置HTTP侦听器 #Setting up the listener $Server = [System.Net.Htt

环境:马科斯

我正在尝试使用
System.Net.HttpListener
创建菜单


是否有任何方法可以使用
[System.Net.WebSocket.ClientWebSocket]$WebSocket
使用Powershell向浏览器客户端发送信息?

您可以这样做,但为了让web服务器向浏览器发送任何信息,浏览器必须启动请求,最常见的情况是使用JavaScript

首先,为服务器设置HTTP侦听器

#Setting up the listener
$Server = [System.Net.HttpListener]::new()
$Server.Prefixes.Add('http://localhost:8001/')
$Server.Start()
这将启动Web服务器侦听指定入口点上的请求。接下来,我们定义一个简短的助手函数,该脚本将使用该函数封装发送回浏览器的响应

Function Send-WebServerResponse($InputObject){
    $JSON = $InputObject | ConvertTo-Json
    $buffer = [System.Text.Encoding]::UTF8.GetBytes($JSON)
    $Context.Response.ContentLength64 = $buffer.Length
    $Context.Response.OutputStream.Write($buffer, 0, $buffer.length)
    $Context.Response.OutputStream.Close()
    }
下一步是编写控制器逻辑,如果这是一个MVC应用程序。基本上是路由层,因此脚本知道如何根据请求的类型进行响应

#Listening for a particular header value
$Context = $Server.GetContext()

    Write-Host "$($Context.Request.UserHostAddress) [$($Context.Request.HttpMethod)]=> $($Context.Request.Url)"  -ForegroundColor Green    

$RequestData = $Context.Request.Headers.GetValues('RequestData')
    if ($Context.Request.Url.AbsolutePath -eq '/TestMe'){
        Write-Host "Received request for /TestMe endpoint..."  -ForegroundColor Green
        $Body = [System.IO.StreamReader]::new($Context.Request.InputStream, $Context.Request.ContentEncoding)
        $Data = $Body.ReadToEnd() | convertfrom-stringdata
        $Body.Close()
        Send-WebServerResponse "Request Received successfully to /TestMe"

    }
最后,以下是JS从该侦听器请求信息的方式:

$.ajax({
            //the url to send the data to
            url: "TestMe",
            //the data to send to
            data: {SomeParam : "SomeValue"},
            //type. for eg: GET, POST
            type: "POST",
            //datatype expected to get in reply form server
            dataType: "json",
            //on success
            success: function(data){
                if (data){
                    Console.Log("Request Success");
                }
                else{
                    Console.Log("Request Failed!");
                }


            },
            //on error
            error: function(){
                //bad request

            }
        });

在@Scottcoro的帮助下写下了这个答案,他教了我这个方法

您可以这样做,但是为了让web服务器向浏览器发送任何信息,浏览器必须启动请求,最常见的情况是使用JavaScript

首先,为服务器设置HTTP侦听器

#Setting up the listener
$Server = [System.Net.HttpListener]::new()
$Server.Prefixes.Add('http://localhost:8001/')
$Server.Start()
这将启动Web服务器侦听指定入口点上的请求。接下来,我们定义一个简短的助手函数,该脚本将使用该函数封装发送回浏览器的响应

Function Send-WebServerResponse($InputObject){
    $JSON = $InputObject | ConvertTo-Json
    $buffer = [System.Text.Encoding]::UTF8.GetBytes($JSON)
    $Context.Response.ContentLength64 = $buffer.Length
    $Context.Response.OutputStream.Write($buffer, 0, $buffer.length)
    $Context.Response.OutputStream.Close()
    }
下一步是编写控制器逻辑,如果这是一个MVC应用程序。基本上是路由层,因此脚本知道如何根据请求的类型进行响应

#Listening for a particular header value
$Context = $Server.GetContext()

    Write-Host "$($Context.Request.UserHostAddress) [$($Context.Request.HttpMethod)]=> $($Context.Request.Url)"  -ForegroundColor Green    

$RequestData = $Context.Request.Headers.GetValues('RequestData')
    if ($Context.Request.Url.AbsolutePath -eq '/TestMe'){
        Write-Host "Received request for /TestMe endpoint..."  -ForegroundColor Green
        $Body = [System.IO.StreamReader]::new($Context.Request.InputStream, $Context.Request.ContentEncoding)
        $Data = $Body.ReadToEnd() | convertfrom-stringdata
        $Body.Close()
        Send-WebServerResponse "Request Received successfully to /TestMe"

    }
最后,以下是JS从该侦听器请求信息的方式:

$.ajax({
            //the url to send the data to
            url: "TestMe",
            //the data to send to
            data: {SomeParam : "SomeValue"},
            //type. for eg: GET, POST
            type: "POST",
            //datatype expected to get in reply form server
            dataType: "json",
            //on success
            success: function(data){
                if (data){
                    Console.Log("Request Success");
                }
                else{
                    Console.Log("Request Failed!");
                }


            },
            //on error
            error: function(){
                //bad request

            }
        });

在@Scottcoro的帮助下写下了这个答案,他教了我这个方法

感谢看起来很有希望,我如何回复“$.ajax”我们在MMS22019、MMSJazz2019和MMS2020上就这一主题进行了一次讨论,如果今年发生的话,甚至可能在MMS2020上进行一次。感谢看起来很有希望,我如何回复“$.ajax”我们在MMS22019、MMSJazz2019上就这一主题进行了一次讨论,如果今年发生的话,甚至可能在MMS2020上进行一次讨论