-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Boris Smus
committed
Mar 18, 2012
1 parent
eeebcd8
commit e229cd2
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var PREV = 20; | ||
var PLAY = 16; | ||
var NEXT = 19; | ||
|
||
function simulateClick(selector) { | ||
var evt = document.createEvent('MouseEvents'); | ||
evt.initMouseEvent('click', true, false, document, 0, 0, 0, 0, 0, false, false, false, false, 0, null); | ||
document.querySelector(selector).dispatchEvent(evt); | ||
} | ||
|
||
var connection = null; | ||
var isConnected = false; | ||
|
||
function connect() { | ||
// Connect to a websocket server | ||
connection = new WebSocket('ws://localhost:1337/'); | ||
|
||
// When the connection is open, send some data to the server | ||
connection.onopen = function() { | ||
console.log('WS open'); | ||
isConnected = true; | ||
connection.send('Ping'); // Send the message 'Ping' to the server | ||
|
||
|
||
// Log errors | ||
connection.onerror = function(error) { | ||
console.log('WS error', error); | ||
}; | ||
|
||
// Log messages from the server | ||
connection.onmessage = function(e) { | ||
console.log('WS message', e); | ||
var key = e.data; | ||
if (key == PREV) { | ||
// Play the previous song | ||
simulateClick('.left_controls .prev'); | ||
} else if (key == NEXT) { | ||
// Play the next song. | ||
simulateClick('.left_controls .next'); | ||
} else if (key == PLAY) { | ||
simulateClick('.left_controls .play_pause'); | ||
} | ||
}; | ||
|
||
connection.onclose = function(e) { | ||
console.log('WS close', e); | ||
isConnected = false; | ||
reconnect(); | ||
}; | ||
}; | ||
|
||
} | ||
|
||
function reconnect() { | ||
// If we're not connected, | ||
if (!isConnected) { | ||
// Attempt to connect. | ||
connect(); | ||
// Then ensure we're connected. | ||
setTimeout(reconnect, 1000); | ||
} | ||
} | ||
|
||
reconnect(); | ||
|
||
|
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