Use php to create a WebSocket service

Source: Internet
Author: User
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.

  1. Include 'websocket. class. php ';
  2. $ Config = array (
  3. 'Address' => '2017. 168.0.200 ',
  4. 'Port' => '123 ',
  5. 'Event' => 'wsevent', // The name of the callback function
  6. 'Log' => true,
  7. );
  8. $ Websocket = new websocket ($ config );
  9. $ Websocket-> run ();
  10. Function WSevent ($ type, $ event ){
  11. Global $ websocket;
  12. If ('in' = $ type ){
  13. $ Websocket-> log ('customer entry id: '. $ event ['K']);
  14. } Elseif ('out' = $ type ){
  15. $ Websocket-> log ('customer exit id: '. $ event ['K']);
  16. } Elseif ('MSG '= $ type ){
  17. $ Websocket-> log ($ event ['K']. 'message:'. $ event ['MSG ']);
  18. Roboot ($ event ['sign'], $ event ['MSG ']);
  19. }
  20. }
  21. Function roboot ($ sign, $ t ){
  22. Global $ websocket;
  23. Switch ($ t)
  24. {
  25. Case 'hello ':
  26. $ Show = 'Hello, GIt @ ossc ';
  27. Break;
  28. Case 'name ':
  29. $ Show = 'Robot ';
  30. Break;
  31. Case 'Time ':
  32. $ Show = 'current Time: '. date ('Y-m-d H: I: s ');
  33. Break;
  34. Case 'Goodbye ':
  35. $ Show = '(^_^ )/~~ Bby ';
  36. $ Websocket-> write ($ sign, 'robot: '. $ show );
  37. $ Websocket-> close ($ sign );
  38. Return;
  39. Break;
  40. Case 'tianwang gaidihu ':
  41. $ Array = array ');
  42. $ Show = $ array [rand (0, 2)];
  43. Break;
  44. Default:
  45. $ Show = '(⊙ o ⊙ ?) Don't understand, you can try to say: hello, name, time, goodbye, the King of the Tiger .';
  46. }
  47. $ Websocket-> write ($ sign, 'robot: '. $ show );
  48. }
  49. ?>

  1. Websocket_TEST
  2. <Li >======= websocket =====< li>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.