alworker:tcp_ipサーバー
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン最新のリビジョン両方とも次のリビジョン | ||
alworker:tcp_ipサーバー [2020/08/30 14:53] – hirohito | alworker:tcp_ipサーバー [2022/02/16 10:25] – hirohito | ||
---|---|---|---|
行 47: | 行 47: | ||
- | クライアント(Aloneライブラリは使用していまません) | + | クライアント(以下の例は、Ruby基本機能のみ使用した例で、Aloneライブラリは使わず実装しています) |
<file ruby tcp_client.rb> | <file ruby tcp_client.rb> | ||
行 78: | 行 78: | ||
===== 解説 ===== | ===== 解説 ===== | ||
- | |||
AlWorkerを継承したクラスに “tcp_” をプレフィックスとして付与したメソッドを定義すると、フレームワークにTCPコマンドとして認識されます。\\ | AlWorkerを継承したクラスに “tcp_” をプレフィックスとして付与したメソッドを定義すると、フレームワークにTCPコマンドとして認識されます。\\ | ||
行 113: | 行 112: | ||
====== chat以外のプロトコルを使用 ====== | ====== chat以外のプロトコルを使用 ====== | ||
- | chat方式ではないプロトコルを使いたい場合は、Tcpクラスを継承して、_start_service()メソッドをオーバライドします。 | + | chat方式ではないプロトコルを使いたい場合は、AlWorker::Tcpクラスを継承して、start_service()メソッドをオーバライドします。 |
すべてのGETリクエストに "It works!" | すべてのGETリクエストに "It works!" | ||
- | <code ruby> | + | <file ruby web_server.rb> |
+ | require " | ||
class WebServer < AlWorker:: | class WebServer < AlWorker:: | ||
- | + | | |
- | | + | |
req = [] | req = [] | ||
while txt = sock.gets | while txt = sock.gets | ||
行 136: | 行 136: | ||
sock.puts "< | sock.puts "< | ||
end | end | ||
- | |||
- | rescue Exception => ex | ||
- | raise ex if ex.class == SystemExit | ||
- | AlWorker.log( ex ) | ||
- | |||
- | ensure | ||
- | sock.close if ! sock.closed? | ||
end | end | ||
end | end | ||
行 148: | 行 141: | ||
class TcpServer < AlWorker | class TcpServer < AlWorker | ||
- | |||
def initialize2() | def initialize2() | ||
- | @tcp = WebServer.new( "< | + | |
+ | | ||
@tcp.run( self ) | @tcp.run( self ) | ||
end | end | ||
- | |||
end | end | ||
- | server = TcpServer.new( " | + | server = TcpServer.new(" |
server.daemon() | server.daemon() | ||
- | </code> | + | </file> |
alworker/tcp_ipサーバー.txt · 最終更新: 2023/08/19 12:06 by hirohito