Skip to content

Instantly share code, notes, and snippets.

@dos65
Created September 6, 2017 13:43
Show Gist options
  • Save dos65/1cdaaa1311b20a974f42caa06f9584df to your computer and use it in GitHub Desktop.
Save dos65/1cdaaa1311b20a974f42caa06f9584df to your computer and use it in GitHub Desktop.
Mist - websocket example
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
<script type="text/javascript">
function startJob(f){
$.ajax({
url:"http://localhost:2004/v2/api/endpoints/simple-context/jobs",
contentType: "application/json",
crossDomain: true,
type: "POST",
data: JSON.stringify({numbers: [1,2,3]}),
success: f
})
}
function tryWs() {
var jobId = "unknown"
var socket = new WebSocket("ws://localhost:2004/v2/api/ws/all");
socket.onopen = function() {
console.log("Connected to mist")
startJob(function(resp) {
jobId = resp.id
console.log("Job id:" + jobId)
})
};
socket.onmessage = function(event) {
var data = JSON.parse(event.data)
if (data.id == jobId) {
console.log("Job event:" + event.data)
}
};
socket.onerror = function(err) {
console.log("Error:" + err)
}
socket.onclose = function(event) {
console.log("Websocket is closed")
console.log(event)
}
}
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment