常用命令:
- 显示启用端口:sbin/iptables -L -n
- 显示进程:sudo ps -ef|grep php
- 终止进程:sudo kill -9 pid 或者 sudo kill -15 pid
- 开放端口的方法:
-
1. 开放端口命令: /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
-
2.保存:/etc/rc.d/init.d/iptables save
-
3.重启服务:/etc/init.d/iptables restart
-
4.查看端口是否开放:/sbin/iptables -L -n
- swoole手册
- 服务端 websocket:
- 注意:
- 端口不能被占用
- 创建websocket服务可参考swoole中快速入门代码
<?php
//创建websocket服务器对象,监听0.0.0.0:9509端口
$ws = new swoole_websocket_server("0.0.0.0", 9509);
//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {
$db = new MySQLi("47.104.70.24","csroot","usercseasy","guoyuan");
$fd=$request->fd;
$ipp = $request->get;
$ip=$ipp['openid'];
$sql = "INSERT INTO testweb (fd,ip) VALUES ('{$fd}','{$ip}')";
$reslut =$db->query($sql);
var_dump($request->fd, $request->get, $request->server);
$data=array($request->fd,'1');
$ws->push($request->fd, 1);
});
//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
echo "Message: {$frame->data}\n";
// var_dump($ws->connections);
// $ws->push($frame->fd, 1);
$grow=$frame->data;
if ($grow<3) {
$grow++;
}
$ws->push($frame->fd, $grow);
foreach ($ws->connections as $key => $value) {
if ($frame->fd !=$value) {
$ws->push($value, $grow);
}
}
});
//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
echo "client-{$fd} is closed\n";
});
$ws->start();
?>
- 浏览器客户端代码
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<img src="jd1.png" id="sss">
<input type="text" name="shu" id="shu" value="">
<button onclick="sendmsg()">浇水</button>
<script src="jquery.js"></script>
<script type="text/javascript">
var wsServer = 'ws://guoyuan.easy-all.cn:9509?openid=123';
var websocket = new WebSocket(wsServer);
//监听链接
websocket.onopen = function (evt) {
console.log(evt);
};
//监听关闭
websocket.onclose = function (evt) {
console.log("Disconnected");
};
//监听服务发送消息
websocket.onmessage = function (evt) {
if (evt.data==2) {
$("#sss").attr('src',"jd2.png");
}
if (evt.data==3) {
$("#sss").attr('src',"jd3.png");
}
console.log(evt);
};
//监听异常
websocket.onerror = function (evt, e) {
console.log('Error occured: ' + evt.data);
};
//js向服务器发送消息
function sendmsg(){
var content = $('#shu').val();
$('#shu').val('')
websocket.send(content);
}
</script>
</body>
</html>
- 小程序客户端代码
// pages/demo/demo.js
Page({
/**
* 页面的初始数据
*/
data: {
status:'1',
websoket:''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
water:function(e){
console.log(1);
var websoket=this.data.websoket;
websoket.send({
data: this.data.status
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
this.data.websoket = wx.connectSocket({
//ws为http请求,wss为https
url:
'ws://47.104.70.24:9509?openid=12345',
})
// wx.onSocketMessage(function (e) {
// console.log(e)
// })
if (this.data.websoket) {
this.data.websoket.onOpen(res => {
console.log('监听 WebSocket 连接打开事件。', res)
})
this.data.websoket.onClose(onClose => {
console.log('监听 WebSocket 连接关闭事件。', onClose)
})
this.data.websoket.onMessage(onMessage => {
this.setData({
status: onMessage.data
})
console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息', onMessage)
})
this.data.websoket.onError(onError => {
console.log('监听 WebSocket 错误。错误信息', onError)
})
// this.data.websoket.onMessage(onMessage => {
// console.log('监听WebSocket接受到服务器的消息事件。服务器返回的消息', onMessage)
// })
}
}