Answer the question
In order to leave comments, you need to log in
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
});
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
}]);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question