-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace socket.io with SockJS as discussed in #229 #302
Changes from 3 commits
7dcd41d
1bc9212
67ff2d5
a5ddf82
75bca73
2dd3058
0ac3e15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,105 @@ | ||
var $ = require("jquery"); | ||
var io = require("socket.io-client"); | ||
var SockJS = require("sockjs-client"); | ||
var stripAnsi = require('strip-ansi'); | ||
require("./style.css"); | ||
|
||
var sock = null; | ||
var hot = false; | ||
var currentHash = ""; | ||
|
||
var newConnection = function(handlers) { | ||
sock = new SockJS('/sockjs-node'); | ||
|
||
sock.onclose = function() { | ||
handlers.close(); | ||
|
||
// Try to reconnect. | ||
sock = null; | ||
setTimeout(function () { | ||
newConnection(handlers); | ||
}, 2000); | ||
}; | ||
|
||
sock.onopen = function() { | ||
// This callback also fires on a reconnect. | ||
handlers.ok(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does a connect fires an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On a normal open There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why not? On the server it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I had a bug in |
||
}; | ||
|
||
sock.onmessage = function(e) { | ||
// This assumes that all data sent via the websocket is JSON. | ||
var msg = JSON.parse(e.data); | ||
handlers[msg.type](msg.data); | ||
}; | ||
}; | ||
|
||
$(function() { | ||
var body = $("body").html(require("./page.jade")()); | ||
var status = $("#status"); | ||
var okness = $("#okness"); | ||
var $errors = $("#errors"); | ||
var iframe = $("#iframe"); | ||
var header = $(".header"); | ||
var hot = false; | ||
var currentHash = ""; | ||
|
||
var contentPage = window.location.pathname.substr("/webpack-dev-server".length) + window.location.search; | ||
|
||
status.text("Connecting to socket.io server..."); | ||
status.text("Connecting to sockjs server..."); | ||
$errors.hide(); iframe.hide(); | ||
header.css({borderColor: "#96b5b4"}); | ||
io = io.connect(); | ||
|
||
io.on("hot", function() { | ||
hot = true; | ||
iframe.attr("src", contentPage + window.location.hash); | ||
}); | ||
|
||
io.on("invalid", function() { | ||
okness.text(""); | ||
status.text("App updated. Recompiling..."); | ||
header.css({borderColor: "#96b5b4"}); | ||
$errors.hide(); if(!hot) iframe.hide(); | ||
}); | ||
|
||
io.on("hash", function(hash) { | ||
currentHash = hash; | ||
}); | ||
|
||
io.on("still-ok", function() { | ||
okness.text(""); | ||
status.text("App ready."); | ||
header.css({borderColor: ""}); | ||
$errors.hide(); if(!hot) iframe.show(); | ||
}); | ||
|
||
io.on("ok", function() { | ||
okness.text(""); | ||
$errors.hide(); | ||
reloadApp(); | ||
}); | ||
|
||
io.on("warnings", function(warnings) { | ||
okness.text("Warnings while compiling."); | ||
$errors.hide(); | ||
reloadApp(); | ||
}); | ||
|
||
io.on("errors", function(errors) { | ||
status.text("App updated with errors. No reload!"); | ||
okness.text("Errors while compiling."); | ||
$errors.text("\n" + stripAnsi(errors.join("\n\n\n")) + "\n\n"); | ||
header.css({borderColor: "#ebcb8b"}); | ||
$errors.show(); iframe.hide(); | ||
}); | ||
|
||
io.on("proxy-error", function(errors) { | ||
status.text("Could not proxy to content base target!"); | ||
okness.text("Proxy error."); | ||
$errors.text("\n" + stripAnsi(errors.join("\n\n\n")) + "\n\n"); | ||
header.css({borderColor: "#ebcb8b"}); | ||
$errors.show(); iframe.hide(); | ||
}); | ||
var onSocketMsg = { | ||
hot: function() { | ||
hot = true; | ||
iframe.attr("src", contentPage + window.location.hash); | ||
}, | ||
invalid: function() { | ||
okness.text(""); | ||
status.text("App updated. Recompiling..."); | ||
header.css({borderColor: "#96b5b4"}); | ||
$errors.hide(); if(!hot) iframe.hide(); | ||
}, | ||
hash: function(hash) { | ||
currentHash = hash; | ||
}, | ||
"still-ok": function() { | ||
okness.text(""); | ||
status.text("App ready."); | ||
header.css({borderColor: ""}); | ||
$errors.hide(); if(!hot) iframe.show(); | ||
}, | ||
ok: function() { | ||
okness.text(""); | ||
$errors.hide(); | ||
reloadApp(); | ||
}, | ||
warnings: function(warnings) { | ||
okness.text("Warnings while compiling."); | ||
$errors.hide(); | ||
reloadApp(); | ||
}, | ||
errors: function(errors) { | ||
status.text("App updated with errors. No reload!"); | ||
okness.text("Errors while compiling."); | ||
$errors.text("\n" + stripAnsi(errors.join("\n\n\n")) + "\n\n"); | ||
header.css({borderColor: "#ebcb8b"}); | ||
$errors.show(); iframe.hide(); | ||
}, | ||
"proxy-error": function(errors) { | ||
status.text("Could not proxy to content base target!"); | ||
okness.text("Proxy error."); | ||
$errors.text("\n" + stripAnsi(errors.join("\n\n\n")) + "\n\n"); | ||
header.css({borderColor: "#ebcb8b"}); | ||
$errors.show(); iframe.hide(); | ||
}, | ||
close: function() { | ||
status.text(""); | ||
okness.text("Disconnected."); | ||
$errors.text("\n\n\n Lost connection to webpack-dev-server.\n Please restart the server to reestablish connection...\n\n\n\n"); | ||
header.css({borderColor: "#ebcb8b"}); | ||
$errors.show(); iframe.hide(); | ||
} | ||
}; | ||
|
||
io.on("disconnect", function() { | ||
status.text(""); | ||
okness.text("Disconnected."); | ||
$errors.text("\n\n\n Lost connection to webpack-dev-server.\n Please restart the server to reestablish connection...\n\n\n\n"); | ||
header.css({borderColor: "#ebcb8b"}); | ||
$errors.show(); iframe.hide(); | ||
}); | ||
newConnection(onSocketMsg); | ||
|
||
iframe.load(function() { | ||
status.text("App ready."); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanted to flag that this breaks certain usage that relied on the previous
/socket.io
path. They now have to be/sockjs-node
. Should this have been a major version bump, according to semver?