claudiograssia / zf2socket
为zend 2的Socket服务器
dev-master
2016-12-13 17:20 UTC
Requires
- hoa/socket: 2.15.*
This package is not auto-updated.
Last update: 2024-09-28 18:21:19 UTC
README
安装
使用Composer安装依赖hoa\socket。
{ "require": { "claudiograssia/zf2socket": "0.0.1-dev" } }
安装后的第一步,在config/application.config.php
中写入
return array( 'modules' => array( "Zf2Socket", "Application", "etc" ) )
使用以下命令在终端中启动测试Socket
myprojectzf2/public php index.php socket test
或者使用
示例用法
将与zendframework 2一起使用。
它将通过控制台路由启动。创建一个控制台如以下示例
module.config.php
return array( 'console' => array( 'router' => array( 'routes' => array( 'indexer' => array( 'options' => array( 'route' => 'socket', 'defaults' => array( 'controller' => 'Application\Controller\Index', 'action' => 'index' ) ) ) ), ), ) )
class IndexController extends AbstractActionController { public function indexAction() { $server = $this->getServerService(); $server->init('tcp://127.0.0.1:4242', function($server) { $line = $server->readLine(); if (empty($line)) { $server->disconnect(); continue; } echo '< ', $line, "\n"; $server->writeLine(strtoupper($line)); }); } /** * @return \Zf2Socket\Service\Server */ protected function getServerService() { return $this->getServiceLocator()->get('SocketServer'); } }
在终端中使用以下命令启动Socket
myprojectzf2/public php index.php socket
并且可以在终端中使用
$ telnet 127.0.0.1 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. test test
或者可以用Python使用
import socket import sys # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect the socket to the port where the server is listening server_address = ('127.0.0.1', 4242) print >>sys.stderr, 'connecting to %s port %s' % server_address sock.connect(server_address)
许可证
这个库是一个免费项目,可以用于任何项目。
注意,还要阅读这个库的许可证依赖。