Non-XML over XMLSocket for F5-F7 Players
DIGG IT!
0
Comments
Published
Friday, October 17, 2003
at
7:20 AM
.
Although XMLSocket implies the use of XML, it is not required. The key to XMLSocket messaging is delimiting using Null bytes or '\0'. Using this technique you can avoid the xml parsing delay and use a string or binary encoding over an XMLSocket. The results are simply fantastic and speeds up system performance by a factor of 5 at a minimum for both client and server. For game use this is essential.
This requires a server that understands the string / binary encoding, here is a small socket server implementation in python:
import socket
HOST = '' # Symbolic name meaning the local host
PORT = 5222 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
data = data[0:len(data)-1]
if not data: break
print "Data received:",data," ",len(data)
if data == 'foo':
print "Data sent:bar"
conn.send('bar\0')
break
On the client side, you use the following to avoid the XML parser:
x = new XMLSocket()
x.onConnect = function(success){
trace('onConnect '+success)
x.send('foo')
}
x.onData = function(x){
trace('onData '+x)
if(x == 'bar'){ trace('bar message!')
}
x.connect('localhost',5222)
The key is in using the onData event vs the onXML event. onData receives a string and onXML receives an XML object. Using onData avoids the XML parser on the client allowing you to use simple string parsing to execute a named function with arguments.
On a current project we are using this technique to render information to screen. The encoding pattern is very simple and supports 3 datatypes(number, string, hex) using space as a delimiter. These values are passed as follows:
x.send('onglyph 23 34 x394393 donut')
The server receives:
'onglyph 23 34 x394393 donut\0'
This is translated to a function call:
onglyph(23,34, 'x394393' , 'donut' )
The results of this function are then returned to the client in the same encoded format.
Although we don't use XML as a whole, that doesn't exclude it from being used as an argument. As XML is just a string, any argument in this encoding can easily be replaced by a XML document. Win, Win for structured data! ;)
Especially in regard to game use, this format is very small, very fast, and very efficient on both the client and server. We have seen great improvements in the performance by using this format. It is also very bandwidth friendly as the message size shrinks dramatically without XML encoding.
Enjoy,
Ted ;)

0 Responses to “ Non-XML over XMLSocket for F5-F7 Players ”
Post a Comment