1 |
|
2 | (function() {
|
3 | var Common, NodeClient, io,
|
4 | extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
5 | hasProp = {}.hasOwnProperty;
|
6 |
|
7 | Common = require("am-common");
|
8 |
|
9 | io = require("socket.io-client/socket.io.js");
|
10 |
|
11 | module.exports = NodeClient = (function(superClass) {
|
12 | extend(NodeClient, superClass);
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | NodeClient.prototype.ws = null;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | NodeClient.prototype.domain = location.host.replace(/:.*/, "");
|
23 |
|
24 | NodeClient.prototype.params = null;
|
25 |
|
26 | NodeClient.prototype.connectFlag = false;
|
27 |
|
28 | function NodeClient() {
|
29 | this.params = this.get_params(location.href);
|
30 | }
|
31 |
|
32 | NodeClient.prototype.start = function() {
|
33 | if (this.params.ws) {
|
34 | return this.connect_websocket();
|
35 | }
|
36 | };
|
37 |
|
38 | NodeClient.prototype.connect_websocket = function() {
|
39 | var protocol;
|
40 | if (this.ws_port === 8080) {
|
41 | if (!location.host.match(/^((192|172|10)\.|localhost)/)) {
|
42 | this.ws_port = 80;
|
43 | }
|
44 | }
|
45 | protocol = location.href.match(/^https/) ? "wss" : "ws";
|
46 | if (this.ws_port === 80) {
|
47 | this.ws_url = protocol + "://" + this.domain;
|
48 | } else {
|
49 | this.ws_url = protocol + "://" + this.domain + ":" + this.ws_port;
|
50 | }
|
51 | this.ws = io(this.ws_url);
|
52 | return this.ws.on("connect", (function(_this) {
|
53 | return function() {
|
54 | if (_this.connectFlag) {
|
55 | return _this.reload();
|
56 | }
|
57 | _this.connectFlag = true;
|
58 | console.log("websocket connected");
|
59 | if (_this.params.g) {
|
60 | _this.ws.emit("g", (typeof _this.params.g === "object" ? _this.params.g : [_this.params.g]));
|
61 | }
|
62 | if (_this.params.all) {
|
63 | _this.ws.emit("all");
|
64 | }
|
65 | _this.ws.on("reload", function() {
|
66 | return _this.reload();
|
67 | });
|
68 | _this.ws.on("css reload", function(css) {
|
69 | return $("body").append("<style type=\"text/css\">" + css + "</style>");
|
70 | });
|
71 | _this.ws.on("message", function(msg) {
|
72 | return console.log(msg);
|
73 | });
|
74 | return _this.ws.on("disconnect", function() {
|
75 | return console.log("websocket server disconnected");
|
76 | });
|
77 | };
|
78 | })(this));
|
79 | };
|
80 |
|
81 | NodeClient.prototype.reload = function() {
|
82 | return location.reload();
|
83 | };
|
84 |
|
85 | return NodeClient;
|
86 |
|
87 | })(Common);
|
88 |
|
89 | }).call(this);
|