查看: 3188|回复: 5

[胡扯] 写了一个用TCP Socket在控制台聊天的东西……

[复制链接]
梦石
0
星屑
1944
在线时间
404 小时
注册时间
2015-8-30
回帖
322
发表于 2018-8-29 20:46:35 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 有丘直方 于 2018-8-29 20:50 编辑

闲着瞎整的玩意儿
由于我只有一台机器,服务器地址是localhost
具体看下面
通用模块
  1. # File: talk_common.rb
  2. require('socket')

  3. module Talk

  4.   HOST = "localhost"
  5.   PORT = 7979
  6.   ANONYM = "Someone"
  7.   CLOSE = "The server has been closed."

  8.   class << self

  9.     def init
  10.     end

  11.     def run
  12.     end

  13.   end

  14.   class TalkServer < TCPServer

  15.     JOIN = "%s joined."
  16.     LEAVE = "%s leaved."
  17.     MESSAGE = "%1$s: %2$s"
  18.     START = "The server has been started."
  19.     MESSAGE_TAG = :message
  20.     NAME_TAG = :name
  21.     LEAVE_TAG = :leave
  22.     MESSAGE_REGEXP = /^<m>(?<#{MESSAGE_TAG}>.*)<\/m>$/
  23.     NAME_REGEXP = /^<n>(?<#{NAME_TAG}>.*)<\/n>$/
  24.     LEAVE_REGEXP = /^<o>(?<#{LEAVE_TAG}>.*)<\/o>$/

  25.     attr_reader(:threads)

  26.     def initialize
  27.       super(PORT)
  28.     end

  29.     def init
  30.       @threads = []
  31.       @clients = []
  32.       push_thread(listen_accept_thread)
  33.       send_message(START)
  34.     end

  35.     def push_thread(thread)
  36.       @threads.push(thread)
  37.     end

  38.     def listen_accept_thread
  39.       Thread.new do
  40.         while client = accept
  41.           @clients.push(client)
  42.           push_thread(listen_talk_thread(client))
  43.         end
  44.       end
  45.     end

  46.     def listen_talk_thread(client)
  47.       Thread.new do
  48.         begin
  49.           while message = client.gets
  50.             args = [client, message]
  51.             client_order(*args, MESSAGE_REGEXP, MESSAGE_TAG, :client_talk)
  52.             client_order(*args, NAME_REGEXP, NAME_TAG, :make_name)
  53.             client_order(*args, LEAVE_REGEXP, LEAVE_TAG, :announce_leave)
  54.           end
  55.         rescue Errno::ECONNRESET
  56.           announce_leave(client)
  57.           Thread.exit
  58.         end
  59.       end
  60.     end

  61.     def send_message(message)
  62.       $stdout.puts(message)
  63.       each_client {|client| client.puts(message) }
  64.     end

  65.     def announce_join(client, arg = nil)
  66.       send_message(sprintf(JOIN, get_name(client)))
  67.     end

  68.     def announce_leave(client, arg = nil)
  69.       send_message(sprintf(LEAVE, get_name(client)))
  70.     end

  71.     def client_talk(client, message)
  72.       send_message(sprintf(MESSAGE, get_name(client), message))
  73.     end

  74.     def get_name(client)
  75.       client.name rescue ANONYM
  76.     end

  77.     def client_order(client, message, regexp, tag, method_name)
  78.       if content = regexp.match(message)
  79.         method(method_name).call(client, content[tag])
  80.       end
  81.     end

  82.     def make_name(client, name)
  83.       index = @clients.index(client)
  84.       client.singleton_class.send(:attr_accessor, :name)
  85.       client.name = name
  86.       announce_join(client)
  87.       @clients[index] = client
  88.     end

  89.     def each_thread(&block)
  90.       @threads.each(&block)
  91.     end

  92.     def each_client(&block)
  93.       @clients.each do |client|
  94.         begin
  95.           block.call(client)
  96.         rescue Errno::ECONNRESET
  97.           next
  98.         end
  99.       end
  100.     end

  101.     def run
  102.       init
  103.       each_thread(&:join)
  104.     end

  105.     def close
  106.       send_message(CLOSE)
  107.       each_thread(&:kill)
  108.       each_client(&:close)
  109.       super
  110.     end

  111.   end

  112.   class TalkClient < TCPSocket

  113.     INPUT_NAME = "Input your name: "
  114.     LEAVE_FORMAT = "<o>%s</o>"
  115.     MESSAGE_FORMAT = "<m>%s</m>"
  116.     NAME_FORMAT = "<n>%s</n>"

  117.     attr_reader(:name)

  118.     def initialize
  119.       super(HOST, PORT)
  120.     end

  121.     def init
  122.       @name = ANONYM
  123.       @thread1 = listen_talk_thread
  124.       @thread2 = listen_send_thread
  125.       input_name
  126.     end

  127.     def input_name
  128.       @name = @@name
  129.       puts(sprintf(NAME_FORMAT, @name))
  130.     end

  131.     def self.input_name
  132.       $stdout.print(INPUT_NAME)
  133.       @@name = $stdin.gets
  134.       @@name[@@name.size - 1] = ""
  135.     end

  136.     def listen_talk_thread
  137.       Thread.new do
  138.         begin
  139.           while message = gets
  140.             $stdout.puts(message)
  141.           end
  142.         rescue
  143.           $stdout.puts(CLOSE)
  144.           Thread.list.each(&exit)
  145.         end
  146.       end
  147.     end

  148.     def listen_send_thread
  149.       Thread.new do
  150.         begin
  151.           while message = $stdin.gets
  152.             message[message.size - 1] = ""
  153.             puts(sprintf(MESSAGE_FORMAT, message))
  154.           end
  155.         rescue
  156.           $stdout.puts(CLOSE)
  157.           Thread.list.each(&exit)
  158.         end
  159.       end
  160.     end

  161.     def close
  162.       puts(LEAVE_FORMAT)
  163.       super
  164.     end

  165.     def run
  166.       init
  167.       @thread1.join
  168.       @thread2.join
  169.     end

  170.   end

  171. end
复制代码
服务端
  1. # File: talk_server.rb
  2. require('talk_common')

  3. class << Talk

  4.   def init
  5.     @server = Talk::TalkServer.new
  6.   end

  7.   def run
  8.     init
  9.     @server.run
  10.   end

  11. end

  12. Talk.run
复制代码
客户端
  1. # File: talk_client.rb
  2. require('talk_common')

  3. class << Talk

  4.   def init
  5.     Talk::TalkClient.input_name
  6.     @client = Talk::TalkClient.new
  7.   end

  8.   def run
  9.     init
  10.     @client.run
  11.   end

  12. end

  13. Talk.run
复制代码
服务端效果 QQ截图20180829203429.png QQ截图20180829203807.png QQ截图20180829204436.png
客户端效果 QQ截图20180829203634.png QQ截图20180829204247.png QQ截图20180829204336.png
小仙女一枚~

老黄鸡

梦石
4
星屑
46133
在线时间
7978 小时
注册时间
2009-7-6
回帖
13342

MZ评测员RM创作大赛01组委会开拓者贵宾

发表于 2018-8-30 08:34:31 | 显示全部楼层
require('socket')

我以为RGSS可用的

点评

工作量大,时间不多  发表于 2018-8-30 19:44
所以为什么黄鸡不把rgd的ruby换个非阉割版啊  发表于 2018-8-30 17:40
我记得有大佬用Win32API做了个Socket可以在RGSS里面用  发表于 2018-8-30 09:42
RGDirect - DirectX驱动的RGSS,点我了解.
(排满,暂停)RM全系列成套系统定制请联系QQ1213237796
不接受对其他插件维护的委托
回复

使用道具 举报

梦石
0
星屑
1803
在线时间
133 小时
注册时间
2013-10-6
回帖
181
发表于 2018-9-3 21:30:26 | 显示全部楼层
foobar.png
我想起来以前用C#做过类似的东西,协议看起来像HTTP/1.X
←你看到一只经常潜水的萌新。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部