Skip to content

Instantly share code, notes, and snippets.

@bil-bas
Created March 18, 2011 17:34
Show Gist options
  • Save bil-bas/876504 to your computer and use it in GitHub Desktop.
Save bil-bas/876504 to your computer and use it in GitHub Desktop.
require 'renet'
class Client
attr_reader :id, :server, :ip
def initialize(server, id, ip)
@server, @id, @ip = server, id, ip
end
def send_packet(data, channel=0, reliable=true)
@server.send_packet(@id, data, reliable, channel)
end
end
def connections_handler(client)
client.on_packet_receive(method(:packets_handler))
client.on_disconnection(method(:disconnections_handler))
client.send_packet("hello")
end
def packets_handler(client, data, channel)
@server.broadcast_packet(data, channel) # Assuming you wanted to echo to all.
@server.each_client {|c| c.send_packet(data, channel) unless c == client } # Assuming you wanted to just route messages.
end
def disconnections_handler(client)
puts "#{client} disconnected"
end
@server = ENet::Server.new(8000, 32, 2, 0, 0)
@server.on_connection(method(:connections_handler))
loop do
@server.update(1000)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment