This experiment on the computer development environment using a jetbrain company phpstorm
Superior following steps:
- Creating websocket server
<? PHP
$ Server = new new swoole_websocket_server ( ' 127.0.0.1 ' , 9502 );
function OnOpen (Serv $, $ Request)
{
echo " successfully opened, already identifier " . $ Request->. FD " open connection \ n " ;
}
$ Server -> ON ( ' Open ' , ' OnOpen ' );
$ Server -> ON ( ' Message ' , function (SW swoole_websocket_server $, $ Frame)
{
echo "opcode operator --- {$ frame-> opcode}; operation data --- {$ frame-> data}; fd data from handle
--- {$ frame-> fd}; finish end symbol {$ frame-> . Finish} \ n- " ;
$ SW -> push (Frame-$> FD, " pushed data " $ Frame-> data.); // data size but 2M
});
$ Server -> ON ( ' Close ' , function (Serv $, $ FD)
{
echo " communication from the end of the {$ fd} \ n- " ;
});
$ Server -> Start ();
>?
- Prepare a html page, and attach websocket protocol script in a page
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <link rel="icon" href="../../../../favicon.ico"> <title>websocket协议</title> <!-- Bootstrap core CSS --> <link href="../css/bootstrap.min.css" rel="stylesheet"> <!-- Custom styles for this template --> <link href="../mycss/sticky-footer-navbar.css" rel="stylesheet"> </head> <body> <header> <!-- Fixed navbar --> <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark"> <a class="navbar-brand" href="#">Fixed navbar</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarCollapse"> <ul class="navbar-nav mr-auto"> <li class="nav-item active"> <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link disabled" href="#">Disabled</a> </li> </ul> <form class="form-inline mt-2 mt-md-0"> <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search"> <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button> </form> </div> </nav> </header> <!-- Begin page content --> <main role="main" class="container"> <h1 class="mt-5">Sticky footer with fixed navbar</h1> <p class="lead">Pin a fixed-height footer to the bottom of the viewport in desktop browsers with this custom HTML and CSS. A fixed navbar has been added with <code>padding-top: 60px;</code> on the <code>body > .container</code>.</p> <p>Back to <a href="../sticky-footer">the default sticky footer</a> minus the navbar.</p> </main> <footer class="footer"> <div class="container"> <span class="text-muted">Place sticky footer content here.</span> </div> </footer> <!-- Bootstrap core JavaScript ================================================== --> <!-- Placed at the end of the document so the pages load faster --> <script src=" ../Js/jquery-3.4.1.min.js " > </ Script> <Script> var myurl = " WS: //swsocks.com: 9502 " ; var WS = new new a WebSocket (myurl); WS. OnOpen = function (EVT) { ws.send ( " Hello server " ); the console.log ( " server hello " ) }; ws.onmessage = function (EVT) { the console.log ( " yo ~ server roar data returned " + evt.data); }; WS.onclose = function (EVT) { the console.log ( " Close up of server-oriented connection " ); } </ Script> </ body> </ HTML>
# --- Compact part WebSocket
<Script> var myurl = " WS: //swsocks.com: 9502 " ; var WS = new new a WebSocket (myurl); ws.onopen = function (EVT) { ws.send ( " Hello Server " ); the console.log ( " server hello " ) }; ws.onmessage = function (EVT) { the console.log ( " yo ~ roar server returns data " + evt.data); }; WS. OnClose = function (EVT) { console.log ( " Close up of server-oriented connection " ); } </ Script>
- In the run-written phpstorm ---- Right-click on the front page of html tab to run, run Run index.html
- operation result
- If you run multiple instances the front page will display more handles
Successfully opened, has an opening for the connection identifier opcode operator --- 1 ; operating data --- hello server; fd data from handle --- 1 ; end symbol 1. Finish successfully opened, already open connection identifier 2 opcode operator --- . 1 ; the operation data --- hello server; fd data from handle --- 2 ; end symbol 1. Finish successfully opened, has to open the connection identifier 3 opcode operator --- . 1 ; the operation data --- hello server; fd data from handle --- . 3 ; 1. Finish end symbol successfully opened, has been opened for the connection identifier. 4 of opcode operator --- . 1 ; the operation data --- hello server; fd handle data from --- . 4 ; 1 Finish end symbol.