k1r0s / krasny

early framework
0 stars 0 forks source link

support websockets #31

Closed k1r0s closed 8 years ago

k1r0s commented 8 years ago

https://www.npmjs.com/package/websocket

http://ahoj.io/nodejs-and-websocket-simple-chat-tutorial

https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications

k1r0s commented 8 years ago

replace whole krasny:start function with this. To start the application when socket connection is ready.

  SELF_KRASNY.start = function (init) {

    var _requiredKeys = ["views", "models", "controllers", "config"];

    _requiredKeys.forEach(_checkRequiredKeys);

    if (SELF_KRASNY.property("config").persistanceAdapter.type === "websocket") {
      var socketUrl = "ws://" + SELF_KRASNY.property("config").persistanceAdapter.url;
      CONS.socket = new WebSocket(socketUrl);
      CONS.socket.onopen = postStart;
    } else if (SELF_KRASNY.property("config").persistanceAdapter.type === "restfull") {
      postStart();
    }
  };

  var postStart = function(){
    SELF_KRASNY.utils._forIn(SELF_KRASNY.property("models"), _createModel);
    SELF_KRASNY.utils._forIn(SELF_KRASNY.property("views"), _createView);
    SELF_KRASNY.utils._retrieveSync(viewTemplates, _bindViewContent,
      function () {
        SELF_KRASNY.utils._forIn(SELF_KRASNY.property("controllers"),
          _prepareRoutes);
        window.location.hash = "/";
        window.onhashchange = _initController;
        _initController({
          newURL: window.location.href
        });
      }
    );
    if (typeof init === "function") init.call(SELF_KRASNY);
  };