Node.js / Java Communication允许多条消息

时间:2012-09-29 09:07:23

标签: java node.js socket.io

我目前在我的服务器上运行了一个java后端,我遇到了一个脚本,用于从Java到Node.js进行通信。我想知道如何修改脚本以一次允许多条消息,如果可能的话,反过来使用它来回传给Java。

以下是代码:

Java Side:

package com.timetablr.backend.nodecommunicator;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.Socket;

public class NodeCommunicator {

public static void main(String[] args) {
    try {
        Socket nodejs = new Socket("localhost", 8080);
        sendMessage(nodejs, "testnamespace");
        Thread.sleep(100);
        int x = 0;
        while (true)
        {
            sendMessage(nodejs, x + "");
            x++;
            Thread.sleep(1000);
            System.out.println(x + " has been sent to server");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}



public static void sendMessage(Socket s, String message) throws IOException {
    s.getOutputStream().write(message.getBytes("UTF-8"));
    s.getOutputStream().flush();
}

public static String readMessage(Socket s) throws IOException {
    InputStream is = s.getInputStream();
    int curr = -1;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while ((curr = is.read()) != -1) {
        if (curr == '\n') {
            break;
        }
        baos.write(curr);
    }
    return baos.toString("UTF-8");
}
}

Node.js Side:

var javaPort = 8080;
var sIOPort = 8081;
var javaServer = require('net').createServer();
var browserServer = require('socket.io').listen(sIOPort);

console.log('=====================================================');
console.log('JamesS237 Timetablr Node.js/Java Communication Module');
console.log('=====================================================');

console.log('Socket.IO version: ' + require('socket.io').version);

javaServer.on('listening', function () {
    console.log('Server is listening on ' + javaPort);
});

javaServer.on('error', function (e) {
    console.log('Server error: ' + e.code);
});

javaServer.on('close', function () {
    console.log('Server closed');
});

javaServer.on('connection', function (javaSocket) {
    var clientAddress = javaSocket.address().address + ':' + javaSocket.address().port;
    console.log('Java ' + clientAddress + ' connected');

    var firstDataListenner = function (data) {
            console.log('Received namespace from java: ' + data);
            console.log(data);
            javaSocket.removeListener('data', firstDataListenner);
            createNamespace(data, javaSocket);
    }

    javaSocket.on('data', firstDataListenner);

    javaSocket.on('close', function() {
            console.log('Java ' + clientAddress + ' disconnected');
    });
});

javaServer.listen(javaPort);

function createNamespace(namespaceName, javaSocket) {
    var browserConnectionListenner = function (browserSocket) {
            console.log('Browser Connected');
            var javaSocketDataListenner = function(data) {
                    console.log('Data received from java socket and sent to browser: ' + data);
                    browserSocket.emit('m', data + '\r\n');
            }

            var javaSocketClosedListenner = function() {
                    console.log('The java socket that was providing data has been closed, removing namespace');
                    browserSocket.disconnect();
                    browserServer.of('/' + namespaceName).removeListener('connection', browserConnectionListenner);
                    javaSocket.removeListener('data', javaSocketDataListenner);
                    javaSocket.removeListener('close', javaSocketClosedListenner);
            }

            javaSocket.on('close', javaSocketClosedListenner);
            javaSocket.on('data', javaSocketDataListenner);

            browserSocket.on('disconnect', function () {
                    console.log('Browser Disconnected');
                    javaSocket.removeListener('data', javaSocketDataListenner);
                    javaSocket.removeListener('close', javaSocketClosedListenner);
            });
    }

    var namespace = browserServer.of('/' + namespaceName).on('connection', browserConnectionListenner);
}

先谢谢,这是一个棘手的问题

0 个答案:

没有答案