Giter Site home page Giter Site logo

Comments (5)

CWyrtzen avatar CWyrtzen commented on June 13, 2024

Hi @billyshena did you find any answers to your question? Have you tried posting in one of the other forums?

Contribution Guide | Stackoverflow | Google Group | Trello | IRC | Build Status

from angularsails.

wadedos avatar wadedos commented on June 13, 2024

Anyone found a solution for initializing the socket in angular? pretty much the same situation as @billyshena. trying to manually initialize the socket after the user has authenticated so I can attach a query string to io.connect. regards.

from angularsails.

gibson avatar gibson commented on June 13, 2024

i make some fixes for this problem, because i have a same))
this version a i dont checked - i change my code for requirejs, but this two methods - like me

<+>UTF-8
===================================================================
--- assets/common/factories/test.js (date 1419454510000)
+++ assets/common/factories/test.js (revision )
@@ -721,8 +721,10 @@

                    // if we won't have the response in cache, send the request to the backend
                    if (angular.isUndefined(cachedResp)) {
-                       $sailsSocketBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
+                       $sailsSocketBackend.then(function (backend) {
+                           backend(config.method, url, reqData, done, reqHeaders, config.timeout,
-                           config.withCredentials, config.responseType);
+                               config.withCredentials, config.responseType);
+                       });
                    }

                    return promise;
@@ -817,8 +819,9 @@
        if(!$window.io.socket){
            console.warn('no connected socket...')
        }
+       var defered = $q.defer();
+       var socket = $window.io.connect();

-
        var tick = function (socket, callback) {
            return callback ? function () {
                var args = arguments;
@@ -843,16 +846,20 @@
            url = url || $browser.url();


-           $window.io.socket[method.toLowerCase()](url,fromJson(post),socketResponse);
+           socket[method.toLowerCase()](url,fromJson(post),socketResponse);

        }

        //TODO normalize http paths to event names
        connection.subscribe = function(event,handler){
-           $window.io.socket.on(event,tick($window.io.socket,handler));
+           socket.on(event,tick(socket,handler));
        }

-       return connection;
+       socket.on('connect', function () {
+           defered.resolve(connection);
+       });
+
+       return defered.promise;

    }

\ No newline at end of file

from angularsails.

gibson avatar gibson commented on June 13, 2024

now when you first make request with $sailsSocket he make connect function and whet socket.io emit connect event - return connection to main code

from angularsails.

twhtanghk avatar twhtanghk commented on June 13, 2024

I run an application into the similar problem. My solution is to configure autoConnect = false and decorate $sailsSocketBackend to return promise for connection ready so that it spares the time for sails.io.js to delete io.socket, assign io.socket once backend is ready, and at last $sailsSocket would raise the request after the backend is ready.

<script type="text/javascript" src="./path/to/bower_components/sails.io.js"></script>
<script type="text/javascript">
io.sails.autoConnect = false;
</script>
angular.module('starter', [...])
    .config ($provide) ->
        $provide.decorator '$sailsSocketBackend', ($delegate, $log) ->
            # socket connect
            io.sails.url = env.server.app.urlRoot
            io.sails.path = "#{env.path}/socket.io"
            io.sails.useCORSRouteToGetCookie = false
            socket = null
            p = new Promise (fulfill, reject) ->
                socket = io.sails.connect()
                socket.on 'connect', ->
                    fulfill()
                socket.on 'connect_error', ->
                    reject()
                socket.on 'connect_timeout', ->
                    reject()

            (method, url, post, callback, headers, timeout, withCredentials, responseType) ->
                p
                    .then ->
                        io.socket = socket
                        opts = 
                            method:     method.toLowerCase()
                            url:        url
                            data:       if typeof post == 'string' then JSON.parse(post) else post
                            headers:    headers
                        socket.request opts, (body, jwr) ->
                            callback jwr.statusCode, body
                    .catch $log.error

from angularsails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.