使用sails.io库将crossdomain-socket.io集成到客户端 [英] Integrate cross-domain-socket.io using sails.io library to client

查看:195
本文介绍了使用sails.io库将crossdomain-socket.io集成到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的服务器上编写了一个基于sails.js的聊天应用程序,所以在客户端的所有代码都在localhost上运行。

I have written a chat application based on sails.js on my server so basiclly all the code in the client side was running on the localhost.

聊天工作像一个梦想,但现在我想把所有的客户端移动到我的cordova应用程序。

the chat is working like a dream but now i want to move all the client side to my cordova application.

这是我的服务器端代码(socket.js)

this is my server side code(socket.js)

onConnect: function(session, socket) {

    // By default, do nothing.
    console.log('hello');
    socket.emit('news', "Welcome to the chat");

    socket.on('send message', function(data){
        console.log(data);
        socket.broadcast.emit('new', data);
        sails.sockets.emit(sails.sockets.id(socket),'new', data);
    });
},

这是我的客户端(服务器localhost)聊天文件:

this is my client side(server localhost) chat file:

$('#form1').submit(function(e){
    e.preventDefault();
    io.socket.emit('send message', $('#userText').val());
    $('#userText').val('');
});

io.socket.on('news', function (msg) {
    console.log(msg);
    $('#content').append(msg + '<br />');
});

io.socket.on('new', function(data){
    console.log(data);
    $('#content').append(data + '<br />');
});

这是客户端(伺服器localhost)要求userCategories API:

and this is client side(server localhost) that request the userCategories API:

io.socket.get('http://178.62.83.248:1337/userCategories', function (resData)    {
   console.log(resData); // => {id:9, name: 'Timmy Mendez'}
});

io.socket.on('usercategories', function(message){
    console.log(message);
});

现在这里是我在cordova客户端(index.js)上尝试的:

now here is what i tried on my cordova client(index.js):

<script src="lib/sails.io.js/sails.io.js"></script>
<script>
    io.sails.url = 'http://178.62.83.248:1337';
    io.socket.get('http://178.62.83.248:1337/userCategories', function (resData) {
      console.log(resData); // => {id:9, name: 'Timmy Mendez'}
    });

    io.socket.on('usercategories', function(message){
        console.log(message);
    });
</script>

此代码的行为是非常奇怪的方式。当应用程序启动时,它尝试连接到: a href =http://178.62.83.248:1337/__getcookie =nofollow> http://178.62.83.248:1337/__getcookie
然后它失败?并在控制台中返回此消息:

this code acts in a very weird way.when the application starts, it tries to connect to: http://178.62.83.248:1337/__getcookie and then it fails? and returns me this message in the console:

        Socket is trying to reconnect to Sails...
 _-|>_-  (attempt #1)

        Socket is trying to reconnect to Sails...
 _-|>_-  (attempt #2)

尝试43,44等...

attempt 43, 44, etc...

可以解释我这是什么意思,我该如何解决这个问题。

so I hope someone can explain me what is this mean and how can I fix this.

推荐答案

发生这种情况的原因是因为当您在客户端上安装sails.io文件时实际安装的是最新版本。为了连接到您的服务器,您需要使用正确的版本的sails io.js.在我的情况下,我在服务器上使用了sails 10.5,所以我必须包括在我的客户端的特定文件

I have solved this problem. The reason this is happening is because when you install sails.io file on your client you actually installing the newest version. In order to connect to your server you need to use the right version of sails io.js. In my case I used sails 10.5 on the server so I had to include the specific file in my client

这篇关于使用sails.io库将crossdomain-socket.io集成到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆