设计思路

使用websocket通信,客户端采用C#开发界面,服务端使用Java开发,最终实现Java服务端向C#客户端发送消息和文件,C#客户端实现语音广播的功能。

Java服务端设计

package servlet.websocket;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint; import servlet.Log; /**
* websocket服务端
*
* @author leibf
*
*/
@ServerEndpoint(value = "/websocket/{clientId}")
public class WebSocketServer {
private final Log log = new Log(WebSocketServer.class);
private Session session;
private String clientId;
private static Map<String, WebSocketServer> clients = new ConcurrentHashMap<String, WebSocketServer>(); // 连接时执行
@OnOpen
public void onOpen(@PathParam("clientId") String clientId, Session session) throws IOException {
this.session = session;
this.clientId = clientId;
clients.put(clientId, this);
log.info("新连接:" + clientId);
} // 关闭时执行
@OnClose
public void onClose(@PathParam("clientId") String clientId, Session session) {
clients.remove(clientId); log.info("连接 " + clientId + " 关闭");
} // 收到消息时执行
@OnMessage
public void onMessage(String message, Session session) throws IOException {
log.info("收到用户的消息: "+ message);
/*if("getMpDefsAndRtDatas".equals(message)){
String msg = UnityServlet.getInstance().getAllMpDefsAndRtDatas();
this.sendMessage(session, msg);
}*/
} // 连接错误时执行
@OnError
public void onError(@PathParam("clientId") String clientId, Throwable error, Session session) {
log.info("用户id为:" + clientId + "的连接发送错误");
error.printStackTrace();
} /**
* 发送消息给某个客户端
* @param message
* @param To
* @throws IOException
*/
public static void sendMessageTo(String message, String To) throws IOException {
for (WebSocketServer item : clients.values()) {
if (item.clientId.equals(To))
item.session.getAsyncRemote().sendText(message);
}
} /**
* 发送消息给某些客户端
* @param message
* @param To
* @throws IOException
*/
public static void sendMessageToSomeone(String message, String To) throws IOException {
for (WebSocketServer item : clients.values()) {
if (item.clientId.startsWith(To))
item.session.getAsyncRemote().sendText(message);
}
} /**
* 发送消息给所有客户端
* @param message
* @throws IOException
*/
public static void sendMessageAll(String message) throws IOException {
for (WebSocketServer item : clients.values()) {
item.session.getAsyncRemote().sendText(message);
}
} /**
* 发送消息
* @param session
* @param message
* @throws IOException
*/
private void sendMessage(Session session,String message) throws IOException{
session.getBasicRemote().sendText(message);
}
}
Java端发送请求指令 String clientId = "broadcast";
try {
WebSocketServer.sendMessageTo("broadcast",clientId);
} catch (IOException e) {
e.printStackTrace();
}

C#客户端设计

websocket连接

WebSocket websocket = null;
private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e){
//接收服务端发来的消息
MessageReceivedEventArgs responseMsg = (MessageReceivedEventArgs)e;
string strMsg = responseMsg.Message;
if(strMsg.Equals("broadcast")){
websocketToPlay();
}else if(strMsg.Equals("broadcastStop")){
websocketToStop(sender,e);
}
} private void websocket_Closed(object sender, EventArgs e){
DisplayStatusInfo("websocket connect failed!");
} private void websocket_Opened(object sender, EventArgs e){
DisplayStatusInfo("websocket connect success!");
} //websocket连接
private void connectWebsocket(){
websocket = new WebSocket("ws://localhost:8080/FrameServlet/websocket/broadcast");
websocket.Opened += websocket_Opened;
websocket.Closed += websocket_Closed;
websocket.MessageReceived += websocket_MessageReceived;
websocket.Open();
}

跨线程操作控件 --- InvokeRequired属性与Invoke方法

private delegate void DoLog(string msg);
private void DisplayStatusInfo(string msg)
{
if (this.InvokeRequired)
{
DoLog doLog = new DoLog(DisplayStatusInfo);
this.Invoke(doLog, new object[] { msg });
}else{
if (msg.Trim().Length > 0)
{
ListBoxStatus.Items.Insert(0, msg);
if (ListBoxStatus.Items.Count > 100)
{
ListBoxStatus.Items.RemoveAt(ListBoxStatus.Items.Count - 1);
}
}
}
}

C#客户端界面展示

Socket通讯-C#客户端与Java服务端通讯(发送消息和文件)的更多相关文章

  1. android客户端向java服务端post发送json

    android 端: private void HttpPostData() {        try { HttpClient httpclient = new DefaultHttpClient( ...

  2. C#使用Thrift简介,C#客户端和Java服务端相互交互

    C#使用Thrift简介,C#客户端和Java服务端相互交互 本文主要介绍两部分内容: C#中使用Thrift简介 用Java创建一个服务端,用C#创建一个客户端通过thrift与其交互. 用纯C#实 ...

  3. RPC学习--C#使用Thrift简介,C#客户端和Java服务端相互交互

    本文主要介绍两部分内容: C#中使用Thrift简介 用Java创建一个服务端,用C#创建一个客户端通过thrift与其交互. 用纯C#实现Client和Server C#服务端,Java客户端 其中 ...

  4. java服务端集成极光消息推送--详细开发步骤

    1.极光推送账号准备 要使用极光消息推送必须先在官方网站上注册账号,并添加应用. 产品介绍:https://docs.jiguang.cn/jpush/guideline/intro/ 注册开发者账号 ...

  5. Unity3D客户端和Java服务端使用Protobuf

    转自:http://blog.csdn.net/kakashi8841/article/details/17334493 前几天有位网友问我关于Unity3D里面使用Protobuf的方法,一时有事拖 ...

  6. 3、netty第二个例子,使用netty建立客户端,与服务端通讯

    第一个例子中,建立了http的服务器端,可以直接使用curl命令,或者浏览器直接访问. 在第二个例子中,建立一个netty的客户端来主动发送请求,模拟浏览器发送请求. 这里先启动服务端,再启动客户端, ...

  7. Socket(TCP)客户端请求和服务端监听和链接基础(附例子)

    一:基础知识回顾 一: Socket 类 实现 Berkeley 套接字接口. Socket(AddressFamily, SocketType,ProtocolType) 使用指定的地址族.套接字类 ...

  8. C++客户端访问Java服务端发布的SOAP模式的WebService接口

    gSOAP是一个绑定SOAP/XML到C/C++语言的工具,使用它可以 简单快速地开发出SOAP/XML的服务器端和客户端 Step1 使用gsoap-2.8\gsoap\bin\win32\wsdl ...

  9. Akka(43): Http:SSE-Server Sent Event - 服务端主推消息

    因为我了解Akka-http的主要目的不是为了有关Web-Server的编程,而是想实现一套系统集成的api,所以也需要考虑由服务端主动向客户端发送指令的应用场景.比如一个零售店管理平台的服务端在完成 ...

随机推荐

  1. OverFeat:基于卷积网络的集成识别、定位与检测

    摘要:我们提出了一个使用卷积网络进行分类.定位和检测的集成框架.我们展示了如何在ConvNet中有效地实现多尺度和滑动窗口方法.我们还介绍了一种新的深度学习方法,通过学习预测对象边界来定位.然后通过边 ...

  2. 详解cocos2dx 3.0的release版本在android平台的签名过程

    当您的游戏准备发布前,需要编译成为release版本,命令中需要增加 -m release,编译命令如下: cocos compile -p android -m release 在编译结束后,生成x ...

  3. (76)深入浅出Mqtt协议

    物联网(Internet of Things,IoT)时代机器之间(Machine-to-Machine,M2M)的大规模沟通需要发布/订阅(Publish/Subscribe)模式,轻量级.易扩展的 ...

  4. 简单地使用webpack进行打包

    之前写的有些零散,现在一步步再重新写.记住: 如果你步骤对,但是始终没成功, 那么请不要烦心, 因为webpack版本4以上, 语义更加严格,命令有一些已经发生改变了,所以并不是你的问题! 一.确保已 ...

  5. What is 'typeof define === 'function' && define['amd']' used for?

    What is 'typeof define === 'function' && define['amd']' used for? This code checks for the p ...

  6. 码云 git 命令提交

    E:\project\eddy-boot-focus>git init E:\project\eddy-boot-focus>git remote add origin https://g ...

  7. 现有1~100 共一百个自然数,已随机放入一个有98个元素的数组a[98].要求写出一个尽量简单的方案找出没有被放入数组的那2个数,并在屏幕上打印这2个数

    void test7() { try { ]; ]; ]; ; ; int i; ; i < num.Length; i++) { num[i] = i + ; num1[i] = i + ;/ ...

  8. golang 导出CSV文件中文乱码的问题

    golang  导出CSV文件中文乱码的问题 解决办法: 在csv文件的开头写入 UTF-8 BOM // 创建文件 dstf, err := os.Create("./data/" ...

  9. Linux 命令 - man 查看命令的文档

    man 命令是 Linux 中最常用的命令,碰到任何让你疑惑的命令,都可以 man 一下来查看详情.不只是 shell 命令,C 语言库函数和系统调用等内容也可以通过 man 命令查看. man 命令 ...

  10. 用seaborn对数据可视化

    以下用sns作为seaborn的别名 1.seaborn整体布局设置 sns.set_syle()函数设置图的风格,传入的参数可以是"darkgrid", "whiteg ...