如何实现 Silverlight 客户端与 Java 应用程序之间的通信





5.00/5 (2投票s)
一个简单的示例,展示了 Silverlight 客户端和 Java 应用程序之间的通信。
引言
下面的示例实现了一个简单的请求-响应式**Silverlight 应用程序和 Java 桌面应用程序之间的通信**。
Silverlight 客户端应用程序发送请求以计算两个数字。Java 服务应用程序接收请求,执行计算并将结果发送回去。
下面的示例使用了 Eneter 消息传递框架,该框架提供了各种通信场景的功能。(该框架是免费的,可以从 http://www.eneter.net 下载。更详细的技术信息可以在 技术信息 处找到。)
策略服务器
策略服务器是一个在 943 端口上监听的特殊服务。该服务接收 <policy-file-request>
并响应一个策略文件,该文件说明允许谁进行通信。
Silverlight 在创建 Tcp 连接时会自动使用此服务。它在 943 端口上发送请求并期望策略文件。如果策略服务器不存在或策略文件的内容不允许通信,则不会创建 Tcp 连接。
Silverlight 客户端应用程序
客户端是一个简单的 Silverlight 应用程序,包含一个按钮,用于发送请求以计算数字。
整个实现非常简单。
using System;
using System.Windows;
using System.Windows.Controls;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Eneter.Messaging.MessagingSystems.TcpMessagingSystem;
namespace CalculatorClient
{
public partial class MainPage : UserControl
{
public class MyRequestMsg
{
public double Number1 { get; set; }
public double Number2 { get; set; }
}
public class MyResponseMsg
{
public double Result { get; set; }
}
// Sender sending MyRequestMsg and receiving responses of type MyResponseMsg.
private IDuplexTypedMessageSender<MyResponseMsg, MyRequestMsg> mySender;
public MainPage()
{
InitializeComponent();
OpenConnection();
}
private void OpenConnection()
{
// Create message sender.
IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory();
mySender = aSenderFactory.CreateDuplexTypedMessageSender<MyResponseMsg, MyRequestMsg>();
// Subscribe to receive response messages.
mySender.ResponseReceived += OnResponseReceived;
// Create TCP messaging.
IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
IDuplexOutputChannel anOutputChannel =
aMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:4502/");
// Attach the output channel to the message sender and be able to
// send messages and receive responses.
mySender.AttachDuplexOutputChannel(anOutputChannel);
}
private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
// Detach the output channel and stop listening to responses.
mySender.DetachDuplexOutputChannel();
}
private void CalculateBtn_Click(object sender, RoutedEventArgs e)
{
// Create the request message.
MyRequestMsg aRequestMsg = new MyRequestMsg();
aRequestMsg.Number1 = Double.Parse(Number1TextBox.Text);
aRequestMsg.Number2 = Double.Parse(Number2TextBox.Text);
// Send request to calculate two numbers.
mySender.SendRequestMessage(aRequestMsg);
}
// It is called when a response message is received.
// Note: By defaulut the response comes in the silverlight thread.
// If it is not desired, the routing to the Silverlight thread can be disabled.
private void OnResponseReceived(object sender, TypedResponseReceivedEventArgs<MyResponseMsg> e)
{
// Display the result.
ResultTextBox.Text = e.ResponseMessage.Result.ToString();
}
}
}
Java 服务应用程序
服务应用程序是一个简单的 Java 应用程序,通过 TCP 进行通信并计算数字。
它还包含用于与 Silverlight 通信的请求的 TCP 策略服务器。
这是整个实现
package calculator;
import java.io.*;
import eneter.messaging.diagnostic.EneterTrace;
import eneter.messaging.endpoints.typedmessages.*;
import eneter.messaging.messagingsystems.messagingsystembase.*;
import eneter.messaging.messagingsystems.tcpmessagingsystem.*;
import eneter.net.system.EventHandler;
public class Program
{
public static class MyRequestMsg
{
public double Number1;
public double Number2;
}
public static class MyResponseMsg
{
public double Result;
}
// Receiver receiving MyResponseMsg and responding MyRequestMsg
private static IDuplexTypedMessageReceiver<MyResponseMsg, MyRequestMsg> myReceiver;
public static void main(String[] args) throws Exception
{
// Start the TCP Policy server.
// Note: Silverlight requests the policy xml to check if the connection
// can be established.
TcpPolicyServer aPolicyServer = new TcpPolicyServer();
aPolicyServer.startPolicyServer();
// Create receiver that receives MyRequestMsg and
// responses MyResponseMsg
IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory();
myReceiver = aReceiverFactory.createDuplexTypedMessageReceiver(
MyResponseMsg.class, MyRequestMsg.class);
// Subscribe to handle incoming messages.
myReceiver.messageReceived().subscribe(myOnMessageReceived);
// Create input channel listening to TCP.
// Note: Silverlight can communicate only on ports: 4502 - 4532
IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
IDuplexInputChannel anInputChannel =
aMessaging.createDuplexInputChannel("tcp://127.0.0.1:4502/");
// Attach the input channel to the receiver and start the listening.
myReceiver.attachDuplexInputChannel(anInputChannel);
System.out.println("Calculator service is running. Press ENTER to stop.");
new BufferedReader(new InputStreamReader(System.in)).readLine();
// Detach the duplex input channel and stop the listening.
// Note: it releases the thread listening to messages.
myReceiver.detachDuplexInputChannel();
// Stop the TCP policy server.
aPolicyServer.stopPolicyServer();
}
private static void onMessageReceived(Object sender, TypedRequestReceivedEventArgs<MyRequestMsg> e)
{
// Calculate incoming numbers.
double aResult = e.getRequestMessage().Number1 + e.getRequestMessage().Number2;
System.out.println(e.getRequestMessage().Number1 + " + " +
e.getRequestMessage().Number2 + " = " + aResult);
// Response back the result.
MyResponseMsg aResponseMsg = new MyResponseMsg();
aResponseMsg.Result = aResult;
try
{
myReceiver.sendResponseMessage(e.getResponseReceiverId(), aResponseMsg);
}
catch (Exception err)
{
EneterTrace.error("Sending the response message failed.", err);
}
}
// Handler used to subscribe for incoming messages.
private static EventHandler<TypedRequestReceivedEventArgs<MyRequestMsg>> myOnMessageReceived
= new EventHandler<TypedRequestReceivedEventArgs<MyRequestMsg>>()
{
@Override
public void onEvent(Object sender, TypedRequestReceivedEventArgs<MyRequestMsg> e)
{
onMessageReceived(sender, e);
}
};
}
这里是应用程序相互通信的情况。