【应用服务 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的启动路径文件。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<webSocket enabled="true" />
<handlers>
<add name="httpplatformhandler" path="*.php" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
</handlers>
<httpPlatform processPath="D:\Program Files (x86)\PHP\v7.4\php.exe"
arguments="D:\home\site\wwwroot\testwebsock.php"
stdoutLogEnabled="true"
stdoutLogFile="D:\home\LogFiles\php-log.txt">
</httpPlatform>
</system.webServer>
</configuration>
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文件,内容如下:
[ExtensionList]
extension=sockets
然后,ini和wwwroot文件夹在kudu站点中的相对位置如下图所示:(必须步骤,非常重要)

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

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

在以上的过程中,必须在App Service的配置页面中启用Web Socket功能。
参考资料
PHP WebSockets:https://github.com/ghedipunk/PHP-Websockets
【应用服务 App Service】在Azure App Service中使用WebSocket - PHP的问题 - 如何使用和调用的更多相关文章
- 【应用服务 App Service】Azure App Service 中如何安装mcrypt - PHP
问题描述 Azure App Service (应用服务)如何安装PHP的扩展 mcrypt(mcrypt 是php里面重要的加密支持扩展库) 准备条件 创建App Service, Runtime ...
- 【Azure媒体服务 Azure Media Service】Azure Media Service中Stream Endpoint 说明 (流式处理终结点)
Azure 媒体服务是一个基于云的媒体工作流平台,用于生成需要编码.打包.内容保护和直播活动广播的解决方案. 在视频的直播,点播方案中,媒体服务的架构主要由三部分构成: 推流端,把本地视频或直播内容推 ...
- 手把手:使用service principal连接Azure Media Service
在简书中查看,请点击我. 关于相关内容解释,请参考docs文档 https://docs.microsoft.com/en-us/azure/media-services/previous/media ...
- 利用VisualVM监测Azure云服务中的Java应用
在做Java开发的时候,我们需要在上线之前对程序进行压力测试,对程序的性能进行全面的监控,了解JVM的CPU,内存,GC,classes,线程等等信息,或者在程序上线运行的过程当中以便于诊断问题或者对 ...
- 【应用服务 App Service】当遇见某些域名在Azure App Service中无法解析的错误,可以通过设置指定DNS解析服务器来解决
问题情形 当访问部署在Azure App Service中的应用返回 "The remote name could not be resolved: ''xxxxxx.com'" ...
- 【Azure 应用服务】App Service for Linux 中实现 WebSocket 功能 (Python SocketIO)
问题描述 使用 python websockets 模块作为Socket的服务端,发布到App Service for Linux环境后,发现Docker Container无法启动.错误消息为: 2 ...
- 【应用服务 App Service】Azure 应用服务测试网络访问其他域名及请求超时限制(4分钟 ≈ 230秒)
测试App Service是否可以访问其他DNS 当应用服务(Azure App Service)创建完成后,想通过ping命令来查看是否可以访问其他站点或解析DNS,但是发现ping命令无法使用.这 ...
- 【Azure App Service For Container】创建ASP.NET Core Blazor项目并打包为Linux镜像发布到Azure应用服务
欢迎使用 Blazor!Blazor 是一个使用 .NET 生成交互式客户端 Web UI 的框架: 使用 C# 代替 JavaScript 来创建信息丰富的交互式 UI. 共享使用 .NET 编写的 ...
- 【Azure 应用服务】Azure App Service 自带 FTP服务
问题描述 Azure PaaS服务是否有FTP/S服务呢? 回答问题 应用服务(Web App/App Service)在创建时候,默认创建了FTP服务并自动开启,用于应用部署.但它不是适合作为FTP ...
随机推荐
- Windows10 安装 CUDA + cuDNN + pyTorch
2020/5/29 在 windows10 上面安装 CUDA 和 cuDNN 0.简单了解一下 CUDA 和 cuDNN 1)什么是 CUDA CUDA(ComputeUnified Device ...
- 万字详解TDengine 2.0整体架构设计思路
导读:涛思数据8月3日将TDengine 的集群功能开源,TDengine具有超强的性能和功能,为什么能做到?它到底有哪些技术创新?今将TDengine的整体设计文档分享出来. 1: 数据模型 物联 ...
- 集群实战(2):K8S集群节点退出加入操作
以下报错网上其实也可以找到并解决,但是偏零碎我只是根据自己的在使用中遇到的问题做个汇总. 文章目录 首先删掉节点 node重新加入 参考文档 首先删掉节点 注意:以下操作都是在master下操作. 一 ...
- openstack (共享组件) 时间同步服务
云计算openstack共享组件——时间同步服务ntp(2) 一.标准时间讲解 地球分为东西十二个区域,共计 24 个时区 格林威治作为全球标准时间即 (GMT 时间 ),东时区以格林威治时区进行 ...
- 一、Git是什么?
Git是什么? Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控 ...
- selenium过豆瓣滑动验证码
首先是加速度代码 def get_tracks(distance): """ 拿到移动轨迹,模仿人的滑动行为,先匀加速后匀减速 匀变速运动基本公式: ①v = v0+at ...
- synchronized 锁的升级
synchronized 的基本认识 在多线程并发编程中 synchronized 一直是元老级角色,很 多人都会称呼它为重量级锁.但是,随着 Java SE 1.6 对 synchronized 进 ...
- idea查询类的继承关系图
方式一:在一个类中,鼠标右键: 结果如下图所示: 方式2:在一个类中 结果如图:
- Spring学习(七)--Spring的AOP
1.实现AOP的方式:通过proxy代理对象.拦截器字码翻译等. 2.AOP体系分层图,从高到低,从使用到实现: 基础:待增强或者目标对象 切面:对基础的增强应用 配置:把基础和切面结合起来,完成切面 ...
- 分页查询对象Page
1 public class Page { 2 //当前页码 3 private Integer pageNo = 1; 4 //每页显示条数 5 private Integer pageSize = ...