diff --git a/integration_test/client/srv_say.rb b/integration_test/client/srv_say.rb new file mode 100755 index 0000000..9031547 --- /dev/null +++ b/integration_test/client/srv_say.rb @@ -0,0 +1,35 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require_relative '../../lib/teeworlds_client' + +client = TeeworldsClient.new + +connected = false + +client.on_connected do |_| + client.rcon_auth(password: 'rcon') + connected = true +end + +# this should nicley print the server message +client.on_chat do |_, msg| + puts "[chat] #{msg}" +end + +# connect and block main thread +client.connect('localhost', 8377, detach: true) + +sleep 0.5 until connected + +puts '[test] sending server hello' +client.rcon('say "hello"') +sleep 1 + +client.rcon('shutdown') +sleep 0.2 + +# this is also testing a disconnect +# if the server shutdown already +# should never crash +client.disconnect diff --git a/integration_test/run.sh b/integration_test/run.sh index 4886cf0..262396c 100755 --- a/integration_test/run.sh +++ b/integration_test/run.sh @@ -160,6 +160,12 @@ then then fail "Error: server still running rcon shutdown failed" fi +elif [ "$testname" == "client/srv_say.rb" ] +then + if ! grep -q '^\[chat\].*hello' "$logdir/ruby_client.txt" + then + fail "Error: missing 'hello' chat message in client log" + fi elif [ "$testname" == "client/multiple_blocks.rb" ] then sleep 1 diff --git a/lib/models/chat_message.rb b/lib/models/chat_message.rb index 2de12c9..4c62eee 100644 --- a/lib/models/chat_message.rb +++ b/lib/models/chat_message.rb @@ -26,6 +26,14 @@ class ChatMesage end def to_s - "#{@author.name}: #{@message}" + # server message + return "*** #{@message}" if @client_id == -1 + + # player message + # should never be from an invalid id + # but lets not crash if servers send weird stuff + name = '' + name = @author.name if @author + "#{name}: #{@message}" end end diff --git a/lib/teeworlds_client.rb b/lib/teeworlds_client.rb index 30df9a5..b5fcdff 100644 --- a/lib/teeworlds_client.rb +++ b/lib/teeworlds_client.rb @@ -446,6 +446,9 @@ class TeeworldsClient # puts "tick" begin pck = @s.recvfrom_nonblock(1400) + rescue Errno::ECONNREFUSED + puts 'connection problems ...' + pck = nil rescue IO::EAGAINWaitReadable pck = nil end