Created
September 1, 2014 21:47
-
-
Save ciarand/a425299b29285a43e112 to your computer and use it in GitHub Desktop.
experiments with websockets and app.net
This file contains hidden or 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
| <!doctype html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>socketdotnet</title> | |
| <meta name="description" content=""> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| </head> | |
| <body> | |
| who even cares | |
| <script src="main.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or 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 url = "wss://stream-channel.app.net/stream/user", | |
| token = "get your own", | |
| conn_id, | |
| base_sub_url = "https://api.app.net/", | |
| make_req, | |
| socket, | |
| logger, | |
| add_sub, | |
| init; | |
| logger = function (str) { | |
| return function (ev) { | |
| console.log(str + " called"); | |
| console.table(ev); | |
| }; | |
| }; | |
| // opens the initial websocket thing | |
| init = function() { | |
| var endpoint = url + | |
| "?access_token=" + token + | |
| "&auto_delete=1"; | |
| socket = new WebSocket(endpoint); | |
| socket.onopen = logger("onopen"); | |
| socket.onclose = logger("onclose"); | |
| socket.onerror = logger("onerror"); | |
| socket.onmessage = function (ev) { | |
| var d = JSON.parse(ev.data); | |
| // if there's no data but there is a meta.connection_id | |
| if (!d.data && d.meta && d.meta.connection_id) { | |
| conn_id = d.meta.connection_id; | |
| add_sub("/posts/stream/unified"); | |
| } else { | |
| logger("onmessage")(d); | |
| } | |
| }; | |
| }; | |
| make_req = function (method, endpoint) { | |
| var req = new XMLHttpRequest(); | |
| req.open(method, endpoint, true); | |
| req.send(); | |
| }; | |
| add_sub = function (api) { | |
| var endpoint = base_sub_url + api + | |
| "?access_token=" + token + | |
| "&connection_id=" + conn_id; | |
| make_req("GET", endpoint); | |
| }; | |
| window.addEventListener("load", init, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment