Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var ourClient;
- var server = 'bee1.shamchat.com';
- function heyItsMeTheIFrame(server, client) {
- ourClient = client;
- }
- // Load a client
- $(document.body).grab(new Element("iframe", {
- src: "http://" + server + "/iframe.html",
- width: 0,
- height: 0,
- frameBorder: 0,
- style: "display:none;"
- }));
- function sendMessage(client, msg) {
- // Ensure we have a client
- if(!ourClient) return;
- // Pack ans send message
- offWithItsHeader(new ourClient.Request({
- url: getRequestURL(server, "/send"),
- data: {
- msg: msg,
- id: client
- }
- })).send()
- };
- function sendDisconnect(client) {
- // Ensure we have a client
- if(!ourClient) return;
- // Pack ans send message
- offWithItsHeader(new ourClient.Request({
- url: getRequestURL(server, "/disconnect"),
- data: {
- id: client
- }
- })).send()
- };
- function makeid() {
- var text = "";
- var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- for( var i=0; i < 10; i++ )
- text += possible.charAt(Math.floor(Math.random() * possible.length));
- return text;
- }
- // Searches for a peer
- var neededPeers = {};
- function findPeer(clientID, callback) {
- var ourID = makeid();
- neededPeers[ourID] = {
- clientID: clientID,
- callback: callback
- }
- sendMessage(clientID, ourID);
- }
- var listenToID;
- function listenTo(clientID) {
- if(clientID) {
- if(!listenToID) {
- listenToID = {};
- }
- listenToID[clientID] = true;
- console.log('Listening to ' + clientID);
- } else {
- listenToID = null;
- }
- }
- function listenToChat(clientID) {
- if(clientID) {
- if(!listenToID) {
- listenToID = {};
- }
- listenToID[clientID] = true;
- // Locate their partner
- findPeer(clientID, function(np, theirID) {
- console.log('Listening to ' + clientID + '\'s partner: ' + theirID);
- listenTo(theirID);
- });
- console.log('Listening to ' + clientID);
- } else {
- listenToID = null;
- }
- }
- // Maps IDs to names
- characterMap = {};
- var ourFaye = new Faye.Client('http://bee1.shamchat.com/faye');
- ourFaye.addExtension({
- incoming:function(message, callback){
- if(message.data){
- message.data.channel = message.channel
- };callback(message);
- }
- });
- ourFaye.disable("autodisconnect");
- ourFaye.subscribe('/*', function(g){
- var clientID = g.channel.substring(1);
- // Autosend a message
- var replyMessage = 'kBye!';
- if(g.event == 'chat') {
- if(g.data != replyMessage) {
- //sendMessage(clientID, replyMessage);
- }
- if(!listenToID || listenToID[clientID]) {
- console.log(clientID + (characterMap[clientID] ? (' (' + characterMap[clientID] + ')') : '') + ': ' + g.data);
- }
- if(neededPeers[g.data]) {
- var np = neededPeers[g.data];
- neededPeers[g.data] = null;
- if (typeof np.callback === "function") {
- np.callback(np, clientID);
- } else {
- console.log(neededPeers[g.data].clientID + ' IS CONNECTED TO ' + clientID);
- }
- }
- }
- if(g.event == 'connect') {
- characterMap[clientID] = g.otherCharacter;
- }
- //sendDisconnect(clientID);
- }, function(e){});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement