服务器的环境,建议用bt.cn的一键环境

服务端:

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019\5\23 0023
* Time: 15:12
*/ class Server{ public function __construct()
{
$sw = new swoole_websocket_server('0.0.0.0',9501); $sw->on('open',function($server, $request){ echo '客户端已经连接'.PHP_EOL;
}); $sw->on('message',function(swoole_websocket_server $server, $frame){ echo PHP_EOL. '服务端已经收到消息';
echo PHP_EOL. "客户端id: " .$frame->fd ; //客户端id
echo PHP_EOL. "接收的消息: " .$frame->data ; //接收的数据
$server->push($frame->fd,"服务把你的信息原封不动的给你了-->".$frame->data); }); $sw->on('close',function($ser, $fd){ echo '连接已经关闭';
}); $sw->start(); } } new Server();

客户端:

<html>
<head>
<title>demo</title>
<script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<input type="text" id="content">
<input type="button" value="send" id="send">
<script type="text/javascript">
var ws = new WebSocket("ws://192.168.56.101:9501");
ws.onopen = function(){
console.log("握手成功");
}
ws.onmessage = function(e){
console.log("message:" + e.data);
}
ws.onerror = function(){
console.log("error");
}
$("#send").click(function(){
content = $("#content").val();
console.log(content);
ws.send(content);
})
</script>
</body>
</html>

结果:

最新文章

  1. CSS系列:CSS文字样式
  2. CSS轮廓outline
  3. VisualSVN Server导入Repository
  4. 分享个win平台cocos2d-x创建项目的快捷方式
  5. [ACM_几何] The Deadly Olympic Returns!!! (空间相对运动之最短距离)
  6. Atitit &#160;&#160;发帖机实现(3 )---usrQBN023 js提交ajax内容到后端规范与标准化
  7. 从window.console&amp;&amp;console.log(123)浅谈JS的且运算逻辑(&amp;&amp;)
  8. 模拟namenode崩溃,使用secondarynamenode恢复
  9. ScrollView--嵌套GridView的解决办法
  10. Javascript Class
  11. SqlServer高版本数据本分还原到低版本方法
  12. 细说javascript 中的 window.open() 参数设置
  13. linux配置Java(JDK)环境变量
  14. 时间转换与星期推算(Matlab版)
  15. Apache Hadoop配置Kerberos指南
  16. Asp.Net Core配置的知识总结
  17. [再寄小读者之数学篇](2014-06-23 积分不等式 [中国科学技术大学2013年高等数学B 考研试题])
  18. iOS原生自动布局NSLayoutConstraint
  19. cocos2dx翻牌效果示例
  20. python 判断两个ip是不是处于同一网段

热门文章

  1. 【前端开发】】ES6属性promise封装js动画
  2. Django请求生命周期和ORM
  3. layui 添加
  4. Pyspark:AssertionError: dataType should be DataType
  5. yum nginx最新版安装
  6. MySQL进阶16 - 视图的创建/修改/删除/更新--可更新性的不适用条件
  7. 【原创】改进的大马webshell,过市面上任何防护
  8. 更新studio 3T的试用期时间
  9. git将一个分支的内容替换为另一分支内容
  10. vue 自定义全局组件