Bah, I seem to talk way too much in this topic.
I wanted to give you guys an update on a Bonjour API I designed. I have not implemented it yet (pending modules) but I tested it.
net.Bonjour {
// domain defaults to "", which results in .local
// type defaults to "_game._tcp"
function Bonjour([type, [domain]])
var type; // String
var domain; // String
var state; // 'publishing', 'published', 'discovering'
function isPublished(); // Boolean
// publish a new service with known type and domain
// name defaults to "" which causes the system to use the computer name
// if no callback is supplied, no notification made about the status
// cb = function(error, status)
function publish([name],port,[cb])
// discover services with known type and domain
// cb is function(error, Bonjour.Peer);
function discover(cb)
// resolve a name to a host and port
// cb = function(error, host, port)
function resolve(name, cb) // to host+port
// Stop publishing, resolving or discovering
function stop();
}
net.Bonjour.Peer {
var name;
// resolve the peer to a host and port
// cb = function(error, host, port)
function resolve(cb)
// returns net.Socket, not connected but intialized
// only connect() will then be needed.
function getSocket();
}
This is not definite! It is just to give you an idea of what the result of such Bonjour API would be.
Usage is very easy:
// Game that will host:
// Create a server
var server = new Server();
server.listen(12345, function(error, status, peerSocket) {
if(error)
throw error;
if(peerSocket) {
console.log("New Peer with address "+peerSocket.remoteAddress);
} else
console.log("Listening!");
});
// Publish the service
var service = new Bonjour();
service.publish("My Awesome Name",12345,function(error,status){
if(error)
throw error;
console.log("Published service!");
});
// Game that wants to play:
var service = new Bonjour();
service.discover(function(error, peer) {
if(error)
throw error;
console.log("Server found: "+peer.name);
// Assume we want the first hit, to omit a UI now
// stop the service
service.stop();
// connect to the peer
var socket = peer.getSocket();
socket.connect();
// ... do whatever
});
Roughly, this creates a listening game server, a client that finds the server automatically and connects to it. No IP address passing, no port number stuff.
That's the idea, at least
// Rahkiin