Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var USERNAME = 'XXXXXX'
- , PASSWORD = 'XXXXXX'
- , sys = require('sys')
- , http = require('http')
- , ws = require('./vendor/ws')
- , base64 = require('./vendor/base64')
- , logger = require("./vendor/logger");
- var a, wait = false;
- // Authentication Headers for Twitter
- var auth = base64.encode(USERNAME + ':' + PASSWORD)
- , headers = {'authorization':'Basic ' + auth, 'host':'betastream.twitter.com', 'content-type':'application/json'};
- // Connection to Twitter's streaming API
- var twitter = http.createClient(443, "betastream.twitter.com", true)
- // , request = twitter.request("GET", "/2b/user.json", headers);
- , request = twitter.request("GET", "/2b/user.json?replies=all", headers);
- request.on('response', function (response) {
- sys.debug("STATUS: " + response.statusCode);
- sys.debug("HEADER: " + JSON.stringify(response.headers));
- response.setEncoding("utf8");
- response.on("data", function (chunk) {
- if ( chunk.substr(0,8) == "{\"friend" && lastchar != '}') { sys.debug("FRIEND: " + chunk); return; } // check for friends header and abort
- if ( chunk.length < 3 ) { return; } // check for heartbeat and abort
- if ( chunk.substr(0,8) == "{\"delete" ) { sys.debug("Delete ####"); return; } // check for delete and abort
- var firstchar = chunk.charAt(0), lastchar = chunk.charAt(chunk.length - 3);
- // START
- if ( firstchar == '{' && lastchar != '}' ) { // looking for partial sends and then combining them
- a = chunk;
- wait = true;
- sys.debug('firstchar ####: ' + firstchar + ' | lastchar ####: ' + lastchar);
- // sys.debug("START ####: " + a);
- sys.debug( "START: " + chunk );
- sys.debug( "AAAAA: " + a );
- return;
- }
- // MIDDLE
- if ( wait && firstchar != '{' && lastchar != '}' ) {
- a += chunk;
- sys.debug('firstchar ####: ' + firstchar + ' | lastchar ####: ' + lastchar);
- // sys.debug("MDDLE ####: " + a);
- sys.debug( "MDDLE: " + chunk );
- sys.debug( "AAAAA: " + a );
- return;
- }
- // FINISH
- if ( wait && firstchar != '{' && lastchar == '}' ) {
- a += chunk;
- sys.debug('firstchar ####: ' + firstchar + ' | lastchar ####: ' + lastchar);
- // sys.debug("FINSH ####: " + a);
- sys.debug( "FINSH: " + chunk );
- sys.debug( "AAAAA: " + a );
- wait = false;
- }
- // sys.debug( "Length: " + chunk.length + " ####" );
- // sys.debug( "Chunk: " + chunk );
- try {
- var check = JSON.parse(chunk);
- if ( check.event == "follow" ) { sys.debug("Follow ####"); return; } // check for follow and abort
- if ( check.event == "list_member_added" ) { sys.debug("List ####"); return; } // check for list and abort
- }
- catch (e) {
- sys.debug("WRONG ###");
- }
- server.broadcast(chunk);
- });
- });
- request.end();
- // Websockets server setup
- var server = ws.createServer({
- debug: true
- , version: "auto"
- });
- server.on("listening", function(){
- sys.debug("Listening for connections.");
- });
- // Handle WebSocket Requests
- server.on("connection", function(conn){
- function log(data){
- sys.debug((+new Date())+" \033[0;32m<"+conn.id+"> "+data.toString()+"\033[0m");
- };
- log("connected");
- server.broadcast("<"+conn.id+"> connected");
- conn.on("close", function(){
- log("onClose");
- server.broadcast("<"+conn.id+"> disconnected");
- });
- conn.on("message", function(message){
- log([message.length, JSON.stringify(message)].join(" | "));
- server.broadcast("<"+conn.id+"> "+message);
- });
- });
- // Handle HTTP Requests:
- server.on("request", function(req, res){
- res.writeHead(200, {'Content-Type': 'text/plain'});
- res.end('This is, infact a websocket server, but we can do http!\n');
- });
- server.listen(8000);
Add Comment
Please, Sign In to add comment