2017-09-22 33 views
0

我想將消息從Meteor應用程序發送到Java應用程序,Meteor應用程序是服務器套接字和Java客戶端;將Meteor中的消息服務器套接字發送到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); 
     } 
    } 
} 

感謝

+0

是否有任何控制檯輸出/錯誤? – Jankapunkt

+0

只是「無法讀取未定義的屬性寫」,使用時:server.connection.write(message +'\ r \ n'); – Rastalovely

+0

我看到,因爲連接不在共享上下文中,所以您的方法不知道連接,因爲服務器是在啓動時創建的變量。將它作爲Meteor.startup之外的變量。 – Jankapunkt

回答

1

爲了使用該方法的連接,你需要使它可用,受外界宣佈它Meteor.startup即我也贊成宣佈在頂級代碼流星方法,以初始化他們在啓動前稱爲:

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'); 
    } 
}); 
+0

你的想法是這樣寫的:server = net.createServer( function(connection){...} ); into method_socket? – Rastalovely

+0

您也可以在方法中調用net.createServer,但是每次調用方法時都會創建服務器和連接,這就是爲什麼您將服務器作爲變量可用的原因,並且在啓動時將其初始化。 – Jankapunkt

相關問題