Skip to content

Instantly share code, notes, and snippets.

@jduff
Forked from glongman/unreal.html
Created May 5, 2010 00:53
Show Gist options
  • Save jduff/390234 to your computer and use it in GitHub Desktop.
Save jduff/390234 to your computer and use it in GitHub Desktop.
<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'></script>
<script>
$(document).ready(function(){
function debug(str){ $("#debug").append("<p>" + str + "</p>"); };
function send_msg_string(str) {
$("#msg").append("<p>[ OUT ]"+str+"</p>");
ws.send(str+"\n");
}
function send_msg() {
var args = Array.prototype.slice.call(arguments);
send_msg_string(args.join("|"));
}
var connected = false;
var ws = new WebSocket("ws://localhost:8080/192.168.0.23");
ws.onmessage = function(evt) {
$("#msg").append("<p>[ IN ] "+evt.data+"</p>");
if (!connected) {
if (evt.data == 'unreal connection lost') {
debug("The connection to unreal failed. We're borked.")
} else {
connected = true
debug("unreal said hello.. starting.");
send_msg("INIT","JSBot","IronGuard","0");
}
} else {
arr = eval(evt.data)
// Do something here stupid.
}
};
ws.onclose = function() { debug("socket closed"); };
ws.onopen = function() {
debug("web socket connected...waiting for unreal to say hello.");
};
});
</script>
</head>
<body>
<div id="debug"></div>
<div id="msg"></div>
</body>
</html>
require 'rubygems'
require 'json' # gem install json
require 'em-websocket' # gem install em-websocket
require 'em-proxy/backend' # gem install em-proxy
DEBUG = true # make the proxy verbose, and probably a bit slower.
# An EM thang that proxies a web socket connection to a UT3Bot backend.
# Server Side
# ruby unreal.rb
#
# Expects that the ws:// url used to make the connection includes the hostname of the UT3Bot server in
# the path part.
# Example: ws://localhost:8080/192.168.3.155
#
# sends UT3Bot messages back through the web socket as a JSON array string.
#
# See the example html file for JS side
#
# Limitations/TODO
# - the example html file is NOT a fully functional bot.
# The JS code a presented will take you as far as connecting to, init-ing with, and receiving
# the initial SPAWNED message from the Unreal server.
# There is a not so politically correct comment in the JS code. That is where you write the
# the 'smarts' for your bot.
# - tested only on a Mac in Chrome 5.0.342.9
# - can't reconnect to the UT2Bot server if the backend connection is lost
# - JS example is very sloppy and could use some JS Guru luv.
#
# Kudos to @igrigorik (http://github.com/igrigorik) for the rockin em-websocket and
# em-proxy gems. And for a blog that is overflowing with awesome (http://www.igvita.com).
class UnrealBackend
def initialize(ws, options = {})
@ws = ws
@debug = options[:debug] || true
@defer = EM::DefaultDeferrable.new
end
def start
if self.host.size == 0
@ws.send("error: no unreal host found in the websocket url path. This connection is toast.")
@defer.callback {@ws.close_connection_after_writing}
else
debug :onopen, "connecting to Unreal"
@server = EventMachine.connect(self.host, 4530, EventMachine::ProxyServer::Backend, @debug) do |c|
c.name = :unreal
c.plexer = self
end
end
end
def stop
@server.close_connection if @server
end
def send(data)
@server.send data
end
def host
return @host if @host
@host = @ws.request['Path']
@host.slice! 0
debug :host, "Host = #{@host.inspect}"
@host
end
def relay_from_backend(name, data)
@ws.send clean_message(data)
end
# called when backend unreal connection is established
def connected(*args)
# do nothing
end
# called when backend unreal connection closes
def unbind_backend(*args)
@ws.send("unreal connection lost")
@defer.callback {ws.close_connection_after_writing}
end
def clean_message(data)
msg = data.strip
msg = $1 if msg =~/(.+)EOM$/
msg.split('|').to_json
end
def debug(*data)
if @debug
require 'pp'
pp data
puts
end
end
end
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => DEBUG) do |ws|
@backend = UnrealBackend.new(ws, :debug => DEBUG)
ws.onopen { @backend.start }
ws.onmessage { |msg| @backend.send msg }
ws.onclose { @backend.stop }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment