Created
April 24, 2015 16:37
-
-
Save humphd/d4021a069f803c0dde78 to your computer and use it in GitHub Desktop.
WebSocket thing
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
| function send(mp3Url, duration) { | |
| var url = "ws://your websocket server url here"; | |
| var msg = { | |
| "play": mp3Url, | |
| "duration": duration | |
| }; | |
| var ws = new WebSocket(url); | |
| ws.onopen = function() { | |
| if(ws.readyState !== ws.OPEN) { | |
| console.error('Unable to send data, websocket not open'); | |
| return; | |
| } | |
| try { | |
| ws.send(JSON.stringify(msg)); | |
| } catch(e) { | |
| console.error('Unable to send data over websocket'); | |
| } | |
| ws.close(); | |
| ws = null; | |
| } | |
| // You may not care about these, but here they are for debugging | |
| ws.onmessage = function(event) { | |
| var data = event.data; | |
| console.log('Received the following data via websocket', data); | |
| }; | |
| ws.onclose = function() { | |
| console.log('Websocket closed'); | |
| ws = null; | |
| }; | |
| ws.onerror = function (err) { | |
| console.error('Websocket error:', err); | |
| } | |
| } | |
| var elem = document.getElementById('id-of-your-element'); | |
| elem.addEventListener('click', function() { | |
| send('some mp3 url', 10) | |
| }, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment