Last active
December 13, 2015 19:38
-
-
Save legastero/4964296 to your computer and use it in GitHub Desktop.
Experimenting with simplest ways to deal with XMPP streams over websockets, in JS.
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
var MAGIC_START_TOKEN = "<stream:stream "; | |
var ALT_MAGIC_START_TOKEN = "<stream "; | |
var MAGIC_END_TOKEN = "</stream:stream>"; | |
var ALT_MAGIC_END_TOKEN = "</stream>"; | |
var STREAM_START_TAG, STREAM_END_TAG; | |
if (frame == MAGIC_END_TOKEN || frame == ALT_MAGIC_END_TOKEN) { | |
onStreamEnd(); | |
} else { | |
var doc; | |
if (!STREAM_START_TAG) { | |
if (frame.startsWith(MAGIC_START_TOKEN) || frame.startsWith(ALT_MAGIC_START_TOKEN) { | |
STREAM_START_TAG = frame; | |
STREAM_END_TAG = frame.startsWith(MAGIC_START_TOKEN) ? MAGIC_END_TOKEN : ALT_MAGIC_END_TOKEN; | |
doc = parseXml(frame + STREAM_END_TAG); | |
if (!checkStreamNS(doc)) { | |
onNSError(); | |
} | |
onStreamStart( doc ); | |
} else { | |
onPrefixError(); | |
} | |
} else { | |
doc = parseXml(STREAM_START_TAG + frame + STREAM_END_TAG); | |
} | |
var stream = doc.documentElement; | |
for (var child = stream.firstChild; child != null; child = child.nextSibling) { | |
onElement(child); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment