【應用服務 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> <View CodehttpPlatform 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=newechoServer("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