使用Java将Meteor中的消息服务器套接字发送到客户端

时间:2017-09-22 17:20:19

标签: java sockets meteor

我想从Meteor应用程序向Java应用程序发送消息,Meteor app是服务器套接字,Java是客户端;

我的目标是在Meteor应用程序的文本框中编写一条消息,然后使用套接字发送给Java。

有人能指出我的代码中的错误吗?

我的代码: 流星

  

my.html

Template.hello.events({
 'click #hola'(event, instance) {
   Meteor.call("method_socket", "say_hello"); // for each click I want to send a message to java
 }
});
  

myServer.js

import { Meteor } from 'meteor/meteor';
var net = require('net');

Meteor.startup(() => {
  // Here is necessary start the server socket
  var net = require('net');
  var server = net.createServer(
    function (connection) {
      console.log('client connect');
      connection.on('end', function () {
        console.log('client end');
      });
      connection.on('error', function () {
        console.log('client error');
      });
      connection.on('close', function () {
        console.log('client close');
      });
      connection.on('data', function (data) {
        console.log("received :", data.toString());
      });
      connection.write('Hello World!\r\n');
      connection.pipe(connection);
    }   
  );

  server.listen(666, function () {
    console.log('server is listening');
  });

  Meteor.methods({
    method_socket: function (message) {
         // I can´t send this message to Java app I had tried:
         server.connection.write(message+'\r\n'); 
          //connection.write(message+'\r\n');
    }
  });

});

我的Java代码:

public static void main(String[] args) {
    Socket smtpSocket = null;
    DataOutputStream os = null;
    DataInputStream is = null;
    PrintStream output;
    try {
        smtpSocket = new Socket("localhost", 666);
        os = new DataOutputStream(smtpSocket.getOutputStream());
        is = new DataInputStream(smtpSocket.getInputStream());
        output = new PrintStream(smtpSocket.getOutputStream());
        output.print("Rastalovely");  //  Send to Meteor this message
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: hostname");
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: hostname");
    }
    if (smtpSocket != null && os != null && is != null) {
        try {
            String responseLine;
            while ((responseLine = is.readLine()) != null) {
                //    wait the response from Meteor
                System.out.println("Server: " + responseLine);
                if (responseLine.indexOf("Ok") != -1) {
                    break;
                }
            }
            os.close();
            is.close();
            smtpSocket.close();
        } catch (UnknownHostException e) {
            System.err.println("Trying to connect to unknown host: " + e);
        } catch (IOException e) {
            System.err.println("IOException:  " + e);
        }
    }
}

由于

1 个答案:

答案 0 :(得分:2)

为了在方法中使用连接,你需要使它可用,即在Meteor.startup之外声明它我也赞成在顶级代码上声明Meteor方法,以便在调用启动之前初始化它们:

var net = require('net');
var server;

Meteor.startup(() => {
  // Here is necessary start the server socket

  server = net.createServer(
    function (connection) {
      //...
    }   
  );

  server.listen(666, function () {
    console.log('server is listening');
  });

});

Meteor.methods({
    method_socket: function (message) {
        // I can´t send this message to Java app I had tried:
        server.connection.write(message+'\r\n'); 
        //connection.write(message+'\r\n');
    }
});
相关问题