As Dan pointed out UI.start_timer works pretty good with UDP. Here's how I'd write it:
require 'socket'
serverSocket = UDPSocket.new
serverSocket.bind("127.0.0.1", 3157)
timer_id = UI.start_timer(0.1, true) {
begin
data = socket.recvfrom_nonblock(100)
new_message = true
rescue Errno;;EWOULDBLOCK
new_message = false
rescue Errno;;ECONNRESET
new_message = false
UI.stop_timer @timer_id
break
end
if new_message == true
UI.messagebox(data)
end
}
The major different is the UI.start_timer in place of the "Thread" and "Loop" in your example. You'll also see that it uses .recvfrom_nonblock with some error catching instead of just .recvfrom. This is because sketchup will hang if nothing is sent to the socket. If you're sure that there will be a constant stream of data you might be able to get it to work with blocking.