【应用服务 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 ...
随机推荐
- 原文https://blog.csdn.net/hongzhen91/article/details/57422897
C语言操作EXCEL文件(读写) 大小宝 2017-02-26 18:18:37 94247 收藏 136展开C语言操作EXCEL文件(读写)本文主要介绍通过纯C语言进行EXCEL的读写操作:(修改时 ...
- JVM运行时数据区--Java虚拟机栈
虚拟机栈的背景 由于跨平台性的设计,java的指令都是根据栈来设计的.不同平台CPU架构不同,所以不能设计为基于寄存器的. 根据栈设计的优点是跨平台,指令集小,编译器容易实现,缺点是性能下降,实现同样 ...
- Docker实战(5)升级Docker版本后的报错
出现情况:因我升级了Centos内核后docker服务无法开启,所做重装处理但还是无效,最终将docker服务做了升级,升级步骤我会放置下面,但在启动老版本容器又出现Error response fr ...
- 4.案例 - NIO实现TCP通信
服务端: package cn.tedu.nio.channel; import java.net.InetSocketAddress; import java.nio.ByteBuffer; imp ...
- Flutter学习二之Dart语言介绍
上次我记录了Flutter的环境搭建,这次来简单记录一下Drat语言,Flutter是 Google推出并开源的移动应用开发框架,开发语言是Dart,那么Dart语言和其他的语言在语法上有上面区别呢, ...
- python安装scrapy库失败
解决方法: 首先,下载Twisted.cp后数字为python版本,例如cp36为python3.6:amd则表示系统位数,例如amd64为64位.下载对应版本即可.点击打开链接 找到Twisted, ...
- php第三天-数组的定义,数组的遍历,常规数组的操作
0x01 数组分类 在php中有两种数组:索引数组和关联数组 索引数组的索引值是整数,以0开始.当通过位置来标识东西时用索引数组. 关联数组是以字符串作为索引值,关联数组更像操作表.索引值为列名,用于 ...
- PHP代码审计01之in_array()函数缺陷
前言 从今天起,结合红日安全写的文章,开始学习代码审计,题目均来自PHP SECURITY CALENDAR 2017,讲完这个题目,会再用一道有相同问题的CTF题来进行巩固.下面开始分析. 漏洞分析 ...
- burp suite 之 proxy(代理)
proxy 代理 通过 Options(选项)的edit(编辑) 更改代理的端口号.我的是10086 (不许更改与本机使用端口冲突的端口号) 使用火狐浏览器将代理更改为10086. 抓取火狐浏览器的包 ...
- Unity Shader学习笔记-1
本篇文章是对Unity Shader入门精要的学习笔记,插图大部分来自冯乐乐女神的github 如果有什么说的不正确的请批评指正 目录 渲染流水线 流程图 Shader作用 屏幕映射 三角形遍历 两大 ...