【应用服务 App Service】在Azure App Service中使用WebSocket - PHP的问题 - 如何使用和调用

简介: 【应用服务 App Service】在Azure App Service中使用WebSocket - PHP的问题 - 如何使用和调用

问题描述

在Azure App Service中,有对.Net,Java的WebSocket支持的示例代码,但是没有成功的PHP代码。 以下的步骤则是如何基于Azure App Service实现PHP版的websocket。

实现步骤

参考PHP代码链接:(GitHub:https://github.com/ghedipunk/PHP-Websockets, 本地博客园:https://www.cnblogs.com/lulight/articles/13787309.html)。但由于这一示例代码中有些错误,所以需要修改部分代码

1) 新加 web.config 文件,配置httpplatformhandler及websocket的启动路径文件。

View Code

2)修改文件testwebsock.php中的内容,$echo = new echoServer("0.0.0.0",getenv("HTTP_PLATFORM_PORT"),1048576); 端口号设置在应用配置参数HTTP_PLATFORM_PORT中。

#!/usr/bin/env php
<?php

require_once('./websockets.php');

class echoServer extends WebSocketServer {

  function __construct($addr, $port, $bufferLength) {
    parent::__construct($addr, $port, $bufferLength);
    $this->userClass = 'MyUser';
  }

  //protected $maxBufferSize = 1048576; //1MB... overkill for an echo server, but potentially plausible for other applications.
  
  protected function process ($user, $message) {
    $this->send($user,$message);
  }
  
  protected function connected ($user) {
    // Do nothing: This is just an echo server, there's no need to track the user.
    // However, if we did care about the users, we would probably have a cookie to
    // parse at this step, would be looking them up in permanent storage, etc.
  }
  
  protected function closed ($user) {
    // Do nothing: This is where cleanup would go, in case the user had any sort of
    // open files or other objects associated with them.  This runs after the socket 
    // has been closed, so there is no need to clean up the socket itself here.
  }
}

$echo = new echoServer("0.0.0.0",getenv("HTTP_PLATFORM_PORT"),1048576);

try {
  $echo->run();
}
catch (Exception $e) {
  $echo->stdout($e->getMessage());
}

3)将client.html 中server URL(var host = "ws://<your web site name>.chinacloudsites.cn/index.php";)改写为对应的website name。

<html><head><title>WebSocket</title>
<style type="text/css">
html,body {
    font:normal 0.9em arial,helvetica;
}
#log {
    width:600px; 
    height:300px; 
    border:1px solid #7F9DB9; 
    overflow:auto;
}
#msg {
    width:400px;
}
</style>
<script type="text/javascript">
var socket;

function init() {
    var host = "ws://xxxxxxxx.chinacloudsites.cn/index.php"; // SET THIS TO YOUR SERVER
    try {
        socket = new WebSocket(host);
        log('WebSocket - status '+socket.readyState);
        socket.onopen    = function(msg) { 
                               log("Welcome - status "+this.readyState); 
                           };
        socket.onmessage = function(msg) { 
                               log("Received: "+msg.data); 
                           };
        socket.onclose   = function(msg) { 
                               log("Disconnected - status "+this.readyState); 
                           };
    }
    catch(ex){ 
        log(ex); 
    }
    $("msg").focus();
}

function send(){
    var txt,msg;
    txt = $("msg");
    msg = txt.value;
    if(!msg) { 
        alert("Message can not be empty"); 
        return; 
    }
    txt.value="";
    txt.focus();
    try { 
        socket.send(msg); 
        log('Sent: '+msg); 
    } catch(ex) { 
        log(ex); 
    }
}
function quit(){
    if (socket != null) {
        log("Goodbye!");
        socket.close();
        socket=null;
    }
}

function reconnect() {
    quit();
    init();
}

// Utilities
function $(id){ return document.getElementById(id); }
function log(msg){ $("log").innerHTML+="<br>"+msg; }
function onkey(event){ if(event.keyCode==13){ send(); } }
</script>

</head>
<body onload="init()">
<h3>WebSocket v2.00</h3>
<div id="log"></div>
<input id="msg" type="textbox" onkeypress="onkey(event)"/>
<button onclick="send()">Send</button>
<button onclick="quit()">Quit</button>
<button onclick="reconnect()">Reconnect</button>
</body>
</html>

4) 添加websocket拓展,早D:\home\site 路径下添加ini文件夹,并在里面添加一个文件名为php.ini文件,内容如下:

1

2

[ExtensionList]

extension=sockets

然后,ini和wwwroot文件夹在kudu站点中的相对位置如下图所示:(必须步骤,非常重要)

 

5)在App Service门户中配置应用参数,PHP_INI_SCAN_DIR="D:\home\site\ini"。 效果如下:

 

 

以上步骤完成后,就可以访问站点下的client.html测试页面,验证此次配置是否成功:

在以上的过程中,必须在App Service的配置页面中启用Web Socket功能

 

参考资料

PHP WebSocketshttps://github.com/ghedipunk/PHP-Websockets

相关文章
|
19天前
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub的解决之法
An exception occurred while retrieving properties for Event Hub: logicapp. Error Message: 'ClientSecretCredential authentication failed: AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud. Che
|
25天前
|
安全
【Azure App Service】App service无法使用的情况分析
App Service集成子网后,如果子网网段中的剩余IP地址非常少的情况下,会在App Service实例升级时( 先加入新实例,然后在移除老实例 )。新加入的实例不能被分配到正确的内网IP地址,无法成功的访问内网资源。 解决方法就是为App Service增加子网地址, 最少需要/26 子网网段地址。
|
1月前
|
C++
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub
|
1月前
【Azure Logic App】在逻辑应用中开启或关闭一个工作流是否会对其它工作流产生影响呢?
【Azure Logic App】在逻辑应用中开启或关闭一个工作流是否会对其它工作流产生影响呢?
|
1月前
|
存储 SQL JSON
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
|
1月前
|
安全 网络安全 Windows
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
|
1月前
|
安全 前端开发 网络安全
【Azure App Service】访问App Service应用报错 SSL: WRONG_VERSION_NUMBER
【Azure App Service】访问App Service应用报错 SSL: WRONG_VERSION_NUMBER
|
1月前
|
Java 应用服务中间件 nginx
【Azure Spring Apps】Spring App部署上云遇见 502 Bad Gateway nginx
【Azure Spring Apps】Spring App部署上云遇见 502 Bad Gateway nginx
|
1月前
|
存储 Linux 网络安全
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
|
1月前
|
开发框架 前端开发 JavaScript
【Azure App Service】.NET应用读取静态文件时遇见了404错误的解决方法
【Azure App Service】.NET应用读取静态文件时遇见了404错误的解决方法