Meteor Server Websockets

醉酒当歌 提交于 2019-12-22 10:46:45

问题


I am looking to create a websocket on Meteor Server (not client) to connect to an external site. I know the URL I am going to be hitting as well as what data to expect, but I am unclear as to how exactly to create the websocket itself. All the searching I do presents me with solutions for the client, but I have yet to run into anything that serves as a server solution.

Is there anything out there I missed that fills this purpose? Atmosherejs.com doesn't list anything, and searching around on google/github didn't reveal anything either. Is there something built into Meteor that already accomplishes this?


回答1:


The following code is for opening a Socket in Meteor on Port 3003. It convert the data from the socket (sendet from client) to a JSON-Object. So this means, the following code is a socket, which receive JSON.

Fiber = Npm.require('fibers')

// server
Npm.require('net').createServer(function (socket) {
    console.log("connected");

    socket.on('data', function (data) {

        socket.write("hello!");

        var o = JSON.parse(data.toString());
        console.log(o);


        Fiber(function() { 
            console.log('Meteor code is executing');
            //=> Meteor code
        }).run();
        //console.log(data.toString());
        //socket.close();
    });
})

.listen(3003);


来源:https://stackoverflow.com/questions/26722120/meteor-server-websockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!