Last active
August 29, 2015 14:02
-
-
Save bontscho/2bcda48a62fee22dac04 to your computer and use it in GitHub Desktop.
POC node-steam joinChat with promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* lib/handlers/friends.js */ | |
var Steam = require('../steam_client'); | |
var SteamID = require('../steamID'); | |
// install q with "npm install q" | |
var Q = require('q'); | |
var EMsg = Steam.EMsg; | |
var schema = Steam.Internal; | |
var protoMask = 0x80000000; | |
// Methods | |
/** snipped **/ | |
prototype.joinChat = function(steamID) { | |
var self = this; | |
// get a new deferred | |
var deferred = Q.defer(); | |
this._send(EMsg.ClientJoinChat, schema.MsgClientJoinChat.serialize({ | |
steamIdChat: toChatID(steamID) | |
})); | |
// custom timeout to fail the join after 10 seconds | |
var timeout = setTimeout(function() { | |
deferred.reject(new Error("Timed out")); | |
self.removeListener('chat:join', resolver); | |
},10000); | |
// quick and dirty resolver that listens to a event and resolves accordingly | |
var resolver = function(data) { | |
// not the channel we are looking for | |
if(data["steamIdClan"] != steamID) { | |
return; | |
} | |
// response successfull, resolve | |
if(data["enterResponse"] == Steam.EChatRoomEnterResponse.Success) { | |
deferred.resolve(data); | |
} else { // reject otherwise | |
deferred.reject(new Error(data["enterResponse"])); | |
} | |
// remove listener and timeout | |
self.removeListener('chat:join', resolver); | |
clearTimeout(timeout); | |
} | |
// listen for chat join events | |
this.on('chat:join', resolver); | |
return deferred.promise; | |
}; | |
/** snipped **/ | |
// Handlers | |
var handlers = prototype._handlers; | |
/** snipped **/ | |
handlers[EMsg.ClientChatEnter] = function(data) { | |
var chatEnter = schema.MsgClientChatEnter.parse(data); | |
// this is the only lined added: | |
this.emit('chat:join', chatEnter); | |
var baseSize = schema.MsgClientChatEnter.baseSize; | |
var numObj = data.readUInt32LE(baseSize); | |
var chatName = data.readCString(baseSize + 4); | |
data = data.slice(baseSize + 4 + Buffer.byteLength(chatName) + 1); | |
var chatRoom = this.chatRooms[chatEnter.steamIdClan || chatEnter.steamIdChat] = {}; | |
while (numObj--) { | |
var object = require('../VDF').parse(data).MessageObject; | |
chatRoom[object.steamid] = { | |
rank: object.Details, | |
permissions: object.permissions | |
}; | |
} | |
}; | |
/** snipped **/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** snipped **/ | |
bot.on('loggedOn', function() { | |
console.log('Bot: Logged in!'); | |
bot.setPersonaState(Steam.EPersonaState.Online); | |
bot.setPersonaName('MyBotName'); | |
bot.on('chatInvite', function(chatID, chatName, inviterID) { | |
// this is the magic of promises | |
bot.joinChat(chatID).then(function(data) { | |
console.log("successfully joined chatroom"); | |
bot.sendMessage(chatID, "Hello chatroom", Steam.EChatEntryType.ChatMsg); | |
// this would list all chatrooms | |
//console.log(bot.chatRooms) | |
}, function(e) { | |
console.log("error", e); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this could obviously be cleaned up, but as a concept it should demonstrate the point.
this could further be expanded by implementing it into the core of the message receiver of the gc and resolve promises according to the jobid in the protobufs