mac平台我在学习websocket的过程中发现我无法console服务器中push回来的数据,甚至练onmessage函数也无法触发
-------------客户端代码-------:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
</head>
<body>
<input type="text" id="content">
<button onclick="exampleSocket.send( document.getElementById('content').value )">发送</button>
<script type="text/javascript">
var exampleSocket = new WebSocket("ws://127.0.0.1:9501");
// Connection opened
exampleSocket.onopen = function()
{
exampleSocket.send("我连接上了");
};
// Listen for messages
exampleSocket.onmessage = function(evt)
{
var msg=JSON.parse(evt.data);
console.log(msg+'1');
};
</script>
</body>
</html>
------服务端的代码------:
<?php
$server = new swoole_websocket_server("127.0.0.1", 9501);
$server->on('open', function (swoole_websocket_server $server, $request) {
echo "server: handshake success with fd{$request->fd}\n";
});
$server->on('message', function (swoole_websocket_server $server, $frame) {
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "this is server",1,true);
#echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
$server->start();
客户端网页图片
但是发送信息后,在控制台并没有触发到console
1 回答
绝地无双
TA贡献1946条经验 获得超4个赞
var msg=JSON.parse(evt.data)
;
不知道你怎么搞的呀!我试了试是可以的呀!
我在服务器上搞的,并不是在本地。客户端的ws地址是我服务器的外网IP,服务器端的地址是 0.0.0.0
并且 $server->push($frame->fd, $frame->data);
后面两个参数我没看不知道是神马意思着。
- 1 回答
- 0 关注
- 667 浏览
添加回答
举报
0/150
提交
取消