我想將消息從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);
}
}
}
感謝
是否有任何控制檯輸出/錯誤? – Jankapunkt
只是「無法讀取未定義的屬性寫」,使用時:server.connection.write(message +'\ r \ n'); – Rastalovely
我看到,因爲連接不在共享上下文中,所以您的方法不知道連接,因爲服務器是在啓動時創建的變量。將它作爲Meteor.startup之外的變量。 – Jankapunkt