编程问答
使用swoole进行消息推送通知,配合vb.net进行客户端开发一样爽[开发篇] -凯发ag旗舰厅登录网址下载
在以前的项目中,就曾听说过swoole的大名,想用来进行消息推送,但是当时只是有了初步的了解,并不敢大胆的运用到线上产品。所谓 识不足则多虑,威不足则多怒、所以就是怕,只能跟领导说了运用极光的推送功能,而且还说出了一定的理由,领导自然也只有相信了,那就用极光推送吧!
最近闲来无事,又重新温习了一下swoole的消息通知功能,虽然在项目开发当时,也曾实现过简单的操作的,但是这次温习更加深了学习,配合vb.net进行开发,感觉还是棒棒哒,下面是一套实现过程,如有需要参考,请拿去,不谢!
首先,我的开发平台是windows,虽然本人也装有virtual box虚拟机,但是那玩意儿太耗内存cpu了,开起来搞实在不想搞。所以,就用了cygwin进行配置开发环境搭建,这里我就不说具体安装步骤了,给个链接吧,一步步按着操作就行了。 查看php在window下安装swoole扩展的方法 请点击。 ps: 如果你win系统里已经安装了一套wamp环境,在cygwin中使用php或其他和win有相同名称命令时,请记得使用全路径 如:/bin/php index.php , /bin/find / -name "init*"
第二步、编写php服务端程序,命名 test_tcp_server.php , 代码如下:
php$serv = new swoole_server("127.0.0.1", 9501);$serv->set(array('worker_num' => 2, //工作进程数量// 'daemonize' => true, //是否作为守护进程,玩玩就不要开启这东西了,不然你都不知道跑到哪里去了 ));$serv->on('connect', 'my_onconnect');$serv->on('receive', 'my_onreceive');$serv->on('close', 'my_onclose');echo "swoole_server is running on 127.0.0.1 9501 \n";$serv->start();//receive msg respondfunction my_onreceive ($serv, $fd, $from_id, $data) {// var_dump($serv);$data = trim($data);// 发送给谁, 如:@id msg... , 最好的当然是使用redis或者数据库存入号码并生成一个映射了,不过玩玩就不要认真了if(substr($data, 0, 1) == '@'){$splitstart = strpos($data, ' ');$touserid = substr($data, 1, $splitstart - 1);echo 'send to:' . $touserid . ';';$content = substr($data, $splitstart 1);if(is_numeric($touserid)){$serv->send($touserid, "from client[$fd@$from_id]:" . $content);}elseif(strtolower($touserid) == 'all'){broadcast($serv, $fd, $content);}else{$serv->send($fd, 'swoole: unkown to user ' . strtolower($touserid) . ' infomation.');}}else{echo "from client[$fd@$from_id],received data: " . $data . php_eol;$serv->send($fd, 'swoole: '.$data);}// $serv->close($fd); }//connect actionfunction my_onconnect ($serv, $fd){echo "client:connect. id:{$fd}\n";}//close actionfunction my_onclose ($serv, $fd) {echo "client: close. id:{$fd}\n";}//broadcast except me, 只会广播from_id相同的用户,再研究了function broadcast(swoole_server $serv, $fd = 0, $data = "hello"){$start_fd = 0;$sendnum = 0;echo "broadcast to ";$from = "from client[$fd]:";while(true){$conn_list = $serv->connection_list($start_fd, 10);if($conn_list === false){break;}var_dump($conn_list);$start_fd = end($conn_list);foreach($conn_list as $conn){if($conn === $fd) continue;$ret1 = $serv->send($conn, $from . $data);$sendnum ;}}echo $sendnum . ' numbers.';}记得把它跑起来哦
/bin/php test_tcp_server.php第三步,你可以编写一个客户端程序,也可以不用编写,因为其他工具也完全可以连接进行的呢!如果要定,可以参考swoole凯发ag旗舰厅登录网址下载官网的tcp_client代码,如下:
php$client = new swoole_client(swoole_sock_tcp, swoole_sock_async);//设置事件回调函数$client->on("connect", function($cli) {$cli->send("hello world\n");});$client->on("receive", function($cli, $data){echo "received: ".$data."\n";});$client->on("error", function($cli){echo "connect failed\n";});$client->on("close", function($cli){echo "connection close\n";});//发起网络连接$client->connect('127.0.0.1', 9501, 0.5);正常的话,运行这个脚本,你就可以收到消息了如: 'from client[1@0],received data: hello,world'
第四步、你肯定不满足于使用php语言去连接服务器端,你当然不应该满足!你还可以使用windows的cmd命令行嘛,如下:
telnet 127.0.0.1 9501然后,你会发现,怎么只输入了一个字符就发送了。原因是什么win的telnet协议之类的,使用send命令,就可以了。操作为:按键 ctrl ] ,进入telnet 命令行模式,输入send aaaddd
ok,多开几个客户端,看起来会话就可以了呢(要查看返回的内容,直接按enter键即可,切换回来使用ctrl ]),来看看服务端都什么样子吧:
都是有响应的吧,ok了,现在再试试一个功能, send @10 hello,10, you ok ? ,则另一个客户端就收到消息了
ok,到此为止,基本上测试完成了。但是使用cmd毕竟看起来不爽,于是想到用vb.net去实现一个客户端。代码就简要的贴出来,看看吧。
第五步,使用vb.net实现客户端访问:
imports system imports system.net imports system.net.sockets imports system.textpublic class synchronoussocketclientprotected shared port as integer = 9501protected shared host as string = "localhost"private shared sender as socketpublic shared bytes(1024) as byte' a way to the connect to the serverpublic shared sub main()if (isnothing(sender)) orelse not sender.connected thenif not isnothing(form1.textbox1.text) thenhost = form1.textbox1.textend ifif (form1.textbox2.text) thenport = form1.textbox2.textend ifsender = connectsocket(host, port)end ifend sub' send something to serverpublic shared sub send()if (isnothing(sender)) orelse not sender.connected thenif not isnothing(form1.textbox1.text) thenhost = form1.textbox1.textend ifif (form1.textbox2.text) thenport = form1.textbox2.textend ifsender = connectsocket(host, port)end if' encode the data string into a byte array.dim msg as byte() = encoding.ascii.getbytes(form1.mycontent.text)' send the data through the socket.dim bytessent as integer = sender.send(msg)' receive the response from the remote device.dim bytesrec as integer = sender.receive(bytes)console.writeline("serverreturn = {0}", encoding.ascii.getstring(bytes, 0, bytesrec))form1.infotext.text &= "serverreturn = " & encoding.ascii.getstring(bytes, 0, bytesrec) & vbnewlineend sub' connect initializeprivate shared function connectsocket(byval host as string, byval port as integer) as socket '创建连接 dim s as socket = nothingdim hostentry as iphostentry = nothing'address = system.net.ipaddress.parse(host)dim iphostinfo as iphostentry = dns.resolve(host)dim address as ipaddress = iphostinfo.addresslist(0)dim endpoint as new ipendpoint(address, port)dim tempsocket as new socket(endpoint.addressfamily, sockettype.stream, protocoltype.tcp)tempsocket.connect(endpoint)if tempsocket.connected thens = tempsocketconsole.writeline("socket connected to {0}", tempsocket.remoteendpoint.tostring())form1.infotext.text &= "socket connected to " & tempsocket.remoteendpoint.tostring()end ifreturn send function' close socketpublic shared sub closesocket()if (sender.connected) thenform1.infotext.text &= "socket closed : " & sender.remoteendpoint.tostring() & vbnewlinesender.shutdown(socketshutdown.both)sender.close()end ifend subend class 'synchronoussocketclient如上是访问连接服务器的代码,触发代码则是在另一个页面进行的,界面如下:
运行后效果如下:
触发事件的代码就比较简单了,还是贴一下吧:
public class form1private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click'console.writeline("console ", rnd) ' 无法读取最后一行console其实 synchronoussocketclient.main()'asynchronousclient.main() ' 异步功能并未实现呢end subprivate sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.loadend subprivate sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.clickinfotext.text = ""end subprivate sub button3_click(byval sender as system.object, byval e as system.eventargs) handles button3.clicksynchronoussocketclient.send()end subprivate sub button4_click(byval sender as system.object, byval e as system.eventargs) handles button4.clicksynchronoussocketclient.closesocket()end sub end class到此,打完,收工。由于异步的调试没有成功,就此打住吧,以后有兴趣再去搞了!
转载于:https://www.cnblogs.com/yougewe/p/5024166.html
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的使用swoole进行消息推送通知,配合vb.net进行客户端开发一样爽[开发篇]的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: