请问socket.io连接响应400代码是什么问题?

ccc1.7正式版,其他版本没测试过。问题如下:

服务器端nodejs:
var exp = require(‘express’);
var app = exp();
var http = require(‘http’).Server(app);
var io = require(‘socket.io’)(http);
app.use(exp.static(’/public’));

io.on('connection', function(socket){
    console.log('a user ' + socket.id);
    socket.emit('connected', 'hello nihao!');
});

http.listen(9000,function(){
    console.log('listening on 9000');
})

新建helloword项目,在helloword.js里修改如下:
onLoad: function () {
this.label.string = this.text;
let self = this;

    var sk = io.connect('192.168.31.246:9000');
    sk.on('connected', function(msg){
        self.label.string = 'cccc';
    });
},

xcode真机连接后提示如下:

JSB SocketIO.connect method called
Calling native SocketIO.connect method
In the constructor of HttpClient!
JSB SocketIO eventName to: ‘connected’
JS: A: 15.974000000000018ms
2017-12-12 22:31:56.358408+0800 hello_world-mobile[3459:357377] Starting to load http://192.168.31.246:9000/socket.io/1/?EIO=2&transport=polling&b64=true
2017-12-12 22:31:56.592451+0800 hello_world-mobile[3459:357377] Received response from request to url http://192.168.31.246:9000/socket.io/1/?EIO=2&transport=polling&b64=true
2017-12-12 22:31:56.657505+0800 hello_world-mobile[3459:357296] :frowning: Websocket Failed With Error Error Domain=SRWebSocketErrorDomain Code=2132 “Received bad response code from server: 400.” UserInfo={NSLocalizedDescription=Received bad response code from server: 400., HTTPResponseStatusCode=400}
Websocket error received: 2
JSB SocketIO::SIODelegate->onClose method called from native
JSB SocketIO::SIODelegate->fireEventToScript method called from native with name ‘disconnect’ data:
disconnect …
2017-12-12 22:31:56.658001+0800 hello_world-mobile[3459:357296] In the destructor of WebSocket-apple (0x1c0234580).
2017-12-12 22:31:56.658258+0800 hello_world-mobile[3459:357296] WebSocketImpl-apple dealloc: 0x1c00a7f80, SRWebSocket ref: 3

请问什么问题?

1赞

郁闷了很多天的问题,今天终于明白是什么问题。

首先是论坛里一开始大神在鼓捣socket.ioCocosCreator + socketIO简易教程(更新至1.0)》,然后官方把这个东东加入到了系统中,应该是至今未更新ccc的socket.io跟上新版本引起的。

看了这篇大神的教程,当时使用的socket.io是1.4.4版本的,而当前官方已经更新至2.0版本了,如果你照着教程并且不加参数npm install --save socket.io用于服务器端和客户端(由于ccc已经内置了,其实可以不用引入,似乎引用了新的还是使用内置的版本,这也是导致为什么与服务器版本对应不上出现400响应的问题吧),你的socket死活都是400错误,可参照这篇美文《node.js socket.io 与 express 集成出错》,于是使用大神范例里的1.4.4版本进行npm install --save socket.io@1.4.4安装node端socket,再照着大神的最后一段代码写客户端,立马可以跑起来了:joy:

@wangzhe 使用socket.io的应该在不少,比如@boyue 麒麟棋牌,ccc的socket.io是不是可以花少少时间升级一下啦。。。。

顶你一下

谢谢@我。 但这个我也不明白。没有遇上过400!

感谢,帮大忙了,我试了一天,找到你的答案终于好了:joy:

这句话救了人

1赞

马克一下

我的项目也遇到了这个问题,但是我是ios外网包才会遇到,架设局域网没问题,我把客户端服务器的socket都降到1.4.4还是不行