B
B
BeArty2015-05-31 08:42:59
Angular
BeArty, 2015-05-31 08:42:59

400 Bad Request when connecting angular client to socket.io server, what to do?

There is a mobile application built on onsen.ui and angular.js.
We need to add a connection to the socket.io server, but when connected, the console gives 400 bad request, and the server responds with {"code":0,"message":"Transport unknown"}.
I tried using https://github.com/btford/angular-socket-io but the result is the same.
There are no problems with pure javascript. It connects as it should and works everything, but with angular...
On the server: LAMP & node.js ([email protected], [email protected], [email protected]).
server app.js

var express = require('express')(),
    http = require('http'),
    app = express,
    server = http.createServer(app),
    io = require('socket.io')(server),
    io = io.listen(server),
    mysql = require('mysql'),
    db = mysql.createConnection({
    host    : '',
    user    : '',
        password: '',
        database: '',
        }),
        userList = []; //Online Users
    
        io.sockets.on('connection', function(socket){
            blablabla
        });
    
        server.listen(3000, function(){
            console.log('Server started on *:3000') //Launch server in 3000 port
        });

client app.js
var app = angular.module('app', ['onsen', 'angular-images-loaded','ngSanitize', 'filters']);
    
        app.factory('socket',function ($rootScope){
            var socket = io.connect('http://server:3000');
            return {
                on: function (eventName,callback){
                    socket.on(eventName,function(){
                        var args = [].slice.call(arguments);
                        $rootScope.$apply(function(){
                            if(callback){
                                callback.apply(socket,args);
                            }
                        });
                    });
                },
                emit: function (eventName, data, callback){
                    var args = [].slice.call(arguments), cb;
                    if( typeof args[args.length-1]  == "function" ){
                        cb = args[args.length-1];
                        args[args.length-1] = function(){
                            var args = [].slice.call(arguments);
                            $rootScope.$apply(function(){
                                if(cb){
                                    cb.apply(socket,args);
                                }
                            });
                        };
                    }
                        socket.emit.apply(socket, args);
                    }
                };
            });
        app.controller('appController', [ '$http', '$scope', '$sce', '$rootScope', function($http, $scope, $sce, $rootScope, socket){
        //conroller code
        }]);

Many thanks in advance for your replies.
PS I tried to ask a question on stackoverflow, but always swears at 4 spaces when formatting the code. I suffered with it and spat without understanding how it works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BeArty, 2015-06-03
@BeArty

No way to go. I did everything in pure javascript

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question