|
1 | 1 | var $ = require("jquery");
|
2 |
| -var io = require("socket.io-client"); |
| 2 | +var SockJS = require("sockjs-client"); |
3 | 3 | var stripAnsi = require('strip-ansi');
|
4 | 4 | require("./style.css");
|
5 | 5 |
|
| 6 | +var sock = null; |
| 7 | +var hot = false; |
| 8 | +var currentHash = ""; |
| 9 | + |
| 10 | +var newConnection = function(handlers) { |
| 11 | + sock = new SockJS('/sockjs-node'); |
| 12 | + |
| 13 | + sock.onclose = function() { |
| 14 | + handlers.close(); |
| 15 | + |
| 16 | + // Try to reconnect. |
| 17 | + sock = null; |
| 18 | + setTimeout(function () { |
| 19 | + newConnection(handlers); |
| 20 | + }, 2000); |
| 21 | + }; |
| 22 | + |
| 23 | + sock.onmessage = function(e) { |
| 24 | + // This assumes that all data sent via the websocket is JSON. |
| 25 | + var msg = JSON.parse(e.data); |
| 26 | + handlers[msg.type](msg.data); |
| 27 | + }; |
| 28 | +}; |
| 29 | + |
6 | 30 | $(function() {
|
7 | 31 | var body = $("body").html(require("./page.jade")());
|
8 | 32 | var status = $("#status");
|
9 | 33 | var okness = $("#okness");
|
10 | 34 | var $errors = $("#errors");
|
11 | 35 | var iframe = $("#iframe");
|
12 | 36 | var header = $(".header");
|
13 |
| - var hot = false; |
14 |
| - var currentHash = ""; |
15 | 37 |
|
16 | 38 | var contentPage = window.location.pathname.substr("/webpack-dev-server".length) + window.location.search;
|
17 | 39 |
|
18 |
| - status.text("Connecting to socket.io server..."); |
| 40 | + status.text("Connecting to sockjs server..."); |
19 | 41 | $errors.hide(); iframe.hide();
|
20 | 42 | header.css({borderColor: "#96b5b4"});
|
21 |
| - io = io.connect(); |
22 |
| - |
23 |
| - io.on("hot", function() { |
24 |
| - hot = true; |
25 |
| - iframe.attr("src", contentPage + window.location.hash); |
26 |
| - }); |
27 | 43 |
|
28 |
| - io.on("invalid", function() { |
29 |
| - okness.text(""); |
30 |
| - status.text("App updated. Recompiling..."); |
31 |
| - header.css({borderColor: "#96b5b4"}); |
32 |
| - $errors.hide(); if(!hot) iframe.hide(); |
33 |
| - }); |
34 |
| - |
35 |
| - io.on("hash", function(hash) { |
36 |
| - currentHash = hash; |
37 |
| - }); |
38 |
| - |
39 |
| - io.on("still-ok", function() { |
40 |
| - okness.text(""); |
41 |
| - status.text("App ready."); |
42 |
| - header.css({borderColor: ""}); |
43 |
| - $errors.hide(); if(!hot) iframe.show(); |
44 |
| - }); |
45 |
| - |
46 |
| - io.on("ok", function() { |
47 |
| - okness.text(""); |
48 |
| - $errors.hide(); |
49 |
| - reloadApp(); |
50 |
| - }); |
51 |
| - |
52 |
| - io.on("warnings", function(warnings) { |
53 |
| - okness.text("Warnings while compiling."); |
54 |
| - $errors.hide(); |
55 |
| - reloadApp(); |
56 |
| - }); |
57 |
| - |
58 |
| - io.on("errors", function(errors) { |
59 |
| - status.text("App updated with errors. No reload!"); |
60 |
| - okness.text("Errors while compiling."); |
61 |
| - $errors.text("\n" + stripAnsi(errors.join("\n\n\n")) + "\n\n"); |
62 |
| - header.css({borderColor: "#ebcb8b"}); |
63 |
| - $errors.show(); iframe.hide(); |
64 |
| - }); |
65 |
| - |
66 |
| - io.on("proxy-error", function(errors) { |
67 |
| - status.text("Could not proxy to content base target!"); |
68 |
| - okness.text("Proxy error."); |
69 |
| - $errors.text("\n" + stripAnsi(errors.join("\n\n\n")) + "\n\n"); |
70 |
| - header.css({borderColor: "#ebcb8b"}); |
71 |
| - $errors.show(); iframe.hide(); |
72 |
| - }); |
| 44 | + var onSocketMsg = { |
| 45 | + hot: function() { |
| 46 | + hot = true; |
| 47 | + iframe.attr("src", contentPage + window.location.hash); |
| 48 | + }, |
| 49 | + invalid: function() { |
| 50 | + okness.text(""); |
| 51 | + status.text("App updated. Recompiling..."); |
| 52 | + header.css({borderColor: "#96b5b4"}); |
| 53 | + $errors.hide(); if(!hot) iframe.hide(); |
| 54 | + }, |
| 55 | + hash: function(hash) { |
| 56 | + currentHash = hash; |
| 57 | + }, |
| 58 | + "still-ok": function() { |
| 59 | + okness.text(""); |
| 60 | + status.text("App ready."); |
| 61 | + header.css({borderColor: ""}); |
| 62 | + $errors.hide(); if(!hot) iframe.show(); |
| 63 | + }, |
| 64 | + ok: function() { |
| 65 | + okness.text(""); |
| 66 | + $errors.hide(); |
| 67 | + reloadApp(); |
| 68 | + }, |
| 69 | + warnings: function(warnings) { |
| 70 | + okness.text("Warnings while compiling."); |
| 71 | + $errors.hide(); |
| 72 | + reloadApp(); |
| 73 | + }, |
| 74 | + errors: function(errors) { |
| 75 | + status.text("App updated with errors. No reload!"); |
| 76 | + okness.text("Errors while compiling."); |
| 77 | + $errors.text("\n" + stripAnsi(errors.join("\n\n\n")) + "\n\n"); |
| 78 | + header.css({borderColor: "#ebcb8b"}); |
| 79 | + $errors.show(); iframe.hide(); |
| 80 | + }, |
| 81 | + "proxy-error": function(errors) { |
| 82 | + status.text("Could not proxy to content base target!"); |
| 83 | + okness.text("Proxy error."); |
| 84 | + $errors.text("\n" + stripAnsi(errors.join("\n\n\n")) + "\n\n"); |
| 85 | + header.css({borderColor: "#ebcb8b"}); |
| 86 | + $errors.show(); iframe.hide(); |
| 87 | + }, |
| 88 | + close: function() { |
| 89 | + status.text(""); |
| 90 | + okness.text("Disconnected."); |
| 91 | + $errors.text("\n\n\n Lost connection to webpack-dev-server.\n Please restart the server to reestablish connection...\n\n\n\n"); |
| 92 | + header.css({borderColor: "#ebcb8b"}); |
| 93 | + $errors.show(); iframe.hide(); |
| 94 | + } |
| 95 | + }; |
73 | 96 |
|
74 |
| - io.on("disconnect", function() { |
75 |
| - status.text(""); |
76 |
| - okness.text("Disconnected."); |
77 |
| - $errors.text("\n\n\n Lost connection to webpack-dev-server.\n Please restart the server to reestablish connection...\n\n\n\n"); |
78 |
| - header.css({borderColor: "#ebcb8b"}); |
79 |
| - $errors.show(); iframe.hide(); |
80 |
| - }); |
| 97 | + newConnection(onSocketMsg); |
81 | 98 |
|
82 | 99 | iframe.load(function() {
|
83 | 100 | status.text("App ready.");
|
|
0 commit comments