0

I have an application that connects to a web page that sends and receives text strings over a websocket on port 1234. I do not have access to the front end code, so I cannot change the HTML front end code. I created an autobahn server with a class derived from WebSocketServer protocol that communicates with the web page over port 1234. This works and I am able to send and receive text to the front end. However, I need to process incoming data and would like to publish the received data to a crossbar.io container through the router on port 8080 (or any other port). The port to the web browser is fixed at 1234. It there a way for me to "plug in " the autobahn websocket server into the crossbar router or is there an alternative way to create a websocket server that will allow me to to send and receive the text on port 1234 and at the same time participate in pub/sub and RPC with the crossbar router?

4

2 回答 2

0

我假设您正在使用 Python。如果不是,答案应该仍然相同,但取决于语言/库及其实现,答案可能会改变。

从你所说的,听起来你并不需要一个“插件”。Crossbar 在路由器组件的描述下确实有这些。但是,除非您真的需要将 Python 实例直接附加到路由器以提高性能或其他原因,否则我建议您将应用程序远离路由器。作为一个独立的实例,它可以很好地工作,特别是如果它位于 WAMP 路由器所在的同一台机器上,数据包只需要通过环回进行通信(这非常快)。

鉴于您使用的是 Python:

您可以一起使用 WebSocketServer 和 WampApplicationServer。您可能会遇到的小问题是正确启动它们。在 Python2.x 和 Twisted 或 Python3.4 和 Asyncio 的任何一种情况下,您只能启动一次反应器/事件循环,否则会出现错误。(Twisted 和 Asyncio 具有相同的基本概念)在 Asyncio 中,RuntimeError: Event loop is running.如果您尝试启动事件循环两次,您将得到。Twisted 也有类似的错误。在 twisted 中使用 ApplicationRunner,有一个选项(在 中的第二个参数run)不启动反应器,您可以在反应器已经运行后使用它。在 Asyncio 中,没有这样的选项,我发现如何做到这一点的唯一方法是继承 Application runner 并覆盖run方法来启动要作为任务启动的会话。另外,请注意,除非正确包装,否则线程不会与任一事件循环合作。

一旦你在一个实例中建立了两个连接,你就可以对数据做任何你想做的事情。

于 2015-03-17T06:34:21.223 回答
0

感谢您的想法,您提到的问题正是我遇到的。然而,我确实找到了一个解决方案,并且由于 crossbar 的灵活性,创建了一个 JavaScript 来宾,它允许我做我需要的事情。这是代码:

// crossbar setup
var autobahn = require('autobahn');

var connection = new autobahn.Connection({
        url: 'ws://127.0.0.1:8080/ws',
        realm: 'realm1'
    }
);

// Websocket to Scratch setup
// pull in the required node packages and assign variables for the entities
var WebSocketServer = require('websocket').server;
var http = require('http');

var ipPort = 1234; // ip port number for Scratch to use

// this connection is a crossbar connection
connection.onopen = function (session) {

    // create an http server that will be used to contain a WebSocket server
    var server = http.createServer(function (request, response) {
        // We are not processing any HTTP, so this is an empty function. 'server' is a wrapper for the
        // WebSocketServer we are going to create below.
    });

    // Create an IP listener using the http server
    server.listen(ipPort, function () {
        console.log('Webserver created and listening on port ' + ipPort);
    });

    // create the WebSocket Server and associate it with the httpServer
    var wsServer = new WebSocketServer({
        httpServer: server
    });

    // WebSocket server has been activated and a 'request' message has been received from client websocket
    wsServer.on('request', function (request) {
        // accept a connection request from Xi4S
        //myconnection is the WS connection to Scratch
        myconnection = request.accept(null, request.origin); // The server is now 'online'

        // Process Xi4S messages
        myconnection.on('message', function (message) {

            console.log('message received: ' + message.utf8Data);
            session.publish('com.serial.data', [message.utf8Data]);

            // Process each message type received
            myconnection.on('close', function (myconnection) {
                console.log('Client closed connection');
                boardReset();
            });
        });
    });
};

connection.open();
于 2015-03-18T13:07:33.150 回答