Use php to create a WebSocket service
Execution method: First, modify the ip address of server.php and index.html. Run the [php path] \ php.exe "[file path] \ server. php" command on the command line" Access the browser to open index.html.
- Include 'websocket. class. php ';
- $ Config = array (
- 'Address' => '2017. 168.0.200 ',
- 'Port' => '123 ',
- 'Event' => 'wsevent', // The name of the callback function
- 'Log' => true,
- );
- $ Websocket = new websocket ($ config );
- $ Websocket-> run ();
- Function WSevent ($ type, $ event ){
- Global $ websocket;
- If ('in' = $ type ){
- $ Websocket-> log ('customer entry id: '. $ event ['K']);
- } Elseif ('out' = $ type ){
- $ Websocket-> log ('customer exit id: '. $ event ['K']);
- } Elseif ('MSG '= $ type ){
- $ Websocket-> log ($ event ['K']. 'message:'. $ event ['MSG ']);
- Roboot ($ event ['sign'], $ event ['MSG ']);
- }
- }
- Function roboot ($ sign, $ t ){
- Global $ websocket;
- Switch ($ t)
- {
- Case 'hello ':
- $ Show = 'Hello, GIt @ ossc ';
- Break;
- Case 'name ':
- $ Show = 'Robot ';
- Break;
- Case 'Time ':
- $ Show = 'current Time: '. date ('Y-m-d H: I: s ');
- Break;
- Case 'Goodbye ':
- $ Show = '(^_^ )/~~ Bby ';
- $ Websocket-> write ($ sign, 'robot: '. $ show );
- $ Websocket-> close ($ sign );
- Return;
- Break;
- Case 'tianwang gaidihu ':
- $ Array = array ');
- $ Show = $ array [rand (0, 2)];
- Break;
- Default:
- $ Show = '(⊙ o ⊙ ?) Don't understand, you can try to say: hello, name, time, goodbye, the King of the Tiger .';
- }
- $ Websocket-> write ($ sign, 'robot: '. $ show );
- }
- ?>
-
- Websocket_TEST
- <Li >======= websocket =====< li>
-
|