Socket通讯-C#客户端与Java服务端通讯(发送消息和文件)
设计思路
使用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服务端通讯(发送消息和文件)的更多相关文章
- android客户端向java服务端post发送json
android 端: private void HttpPostData() { try { HttpClient httpclient = new DefaultHttpClient( ...
- C#使用Thrift简介,C#客户端和Java服务端相互交互
C#使用Thrift简介,C#客户端和Java服务端相互交互 本文主要介绍两部分内容: C#中使用Thrift简介 用Java创建一个服务端,用C#创建一个客户端通过thrift与其交互. 用纯C#实 ...
- RPC学习--C#使用Thrift简介,C#客户端和Java服务端相互交互
本文主要介绍两部分内容: C#中使用Thrift简介 用Java创建一个服务端,用C#创建一个客户端通过thrift与其交互. 用纯C#实现Client和Server C#服务端,Java客户端 其中 ...
- java服务端集成极光消息推送--详细开发步骤
1.极光推送账号准备 要使用极光消息推送必须先在官方网站上注册账号,并添加应用. 产品介绍:https://docs.jiguang.cn/jpush/guideline/intro/ 注册开发者账号 ...
- Unity3D客户端和Java服务端使用Protobuf
转自:http://blog.csdn.net/kakashi8841/article/details/17334493 前几天有位网友问我关于Unity3D里面使用Protobuf的方法,一时有事拖 ...
- 3、netty第二个例子,使用netty建立客户端,与服务端通讯
第一个例子中,建立了http的服务器端,可以直接使用curl命令,或者浏览器直接访问. 在第二个例子中,建立一个netty的客户端来主动发送请求,模拟浏览器发送请求. 这里先启动服务端,再启动客户端, ...
- Socket(TCP)客户端请求和服务端监听和链接基础(附例子)
一:基础知识回顾 一: Socket 类 实现 Berkeley 套接字接口. Socket(AddressFamily, SocketType,ProtocolType) 使用指定的地址族.套接字类 ...
- C++客户端访问Java服务端发布的SOAP模式的WebService接口
gSOAP是一个绑定SOAP/XML到C/C++语言的工具,使用它可以 简单快速地开发出SOAP/XML的服务器端和客户端 Step1 使用gsoap-2.8\gsoap\bin\win32\wsdl ...
- Akka(43): Http:SSE-Server Sent Event - 服务端主推消息
因为我了解Akka-http的主要目的不是为了有关Web-Server的编程,而是想实现一套系统集成的api,所以也需要考虑由服务端主动向客户端发送指令的应用场景.比如一个零售店管理平台的服务端在完成 ...
随机推荐
- Android中实现双击(多击)事件
要实现双击,你需要保存第一次点击时的时间,需要使用到变量,之后便是与第二次点击时的时间比较,看时间间隔是否在你设定的时间内(比如500ms). ? 1 2 3 4 5 6 7 8 9 10 11 12 ...
- bootstrap的editTable实现方法
首先下载基于bootstrap的源码到本地.引用相关文件. <link href="/Content/bootstrap/css/bootstrap.min.css" rel ...
- 大数据笔记(二十四)——Scala面向对象编程实例
===================== Scala语言的面向对象编程 ======================== 一.面向对象的基本概念:把数据和操作数据的方法放到一起,作为一个整体(类 c ...
- SRCNN 卷积神经网络
2019-05-19 从GitHub下载了代码(这里) 代码量虽然不多,但是第一次学,花了时间还是挺多的.根据代码有跑出结果(基本没有改),但是对于数据集的处理还是看的很懵逼,主要是作者的实现都是用类 ...
- MyRocks安装部署
参考:https://www.cnblogs.com/WonderHow/p/5621591.html CentOS 7.3 gflags:git clone https://github.com/g ...
- FreeBSD上安装Cassandra 3.10
哈哈,你居然点进来了,来吧,一起吐槽FreeBSD啊,装了一上午Cassandra 3.10都没有装成功, 终于,鄙人一条 shutdown -p now 结束了FreeBSD,默默打开了CentOS ...
- java网络通信:伪异步I/O编程(PIO)
缺点:避免了线程资源耗尽的问题,但是根本上来说,serversocket的accept方法和inputstream的输入流方法都是阻塞型方法. 服务端:加了一个线程池,实现线程复用.客户端不变 pub ...
- 看看 Delphi XE2 为 VCL 提供的 14 种样式
看看 Delphi XE2 为 VCL 提供的 14 种样式 其实只提供了 13 个 vsf 样式文件, 还有默认的 Windows 样式, 共 14 种. 在空白窗体上添加 ListBox1 等控件 ...
- 递归算法输出数列的前N个数
数列1,1,1,3,5,9,17,31,57,105……N大于3时,第N个数为前三个数之和. ; i < ; i++) { listint.Add(); } test3(); test3(); ...
- 应用安全 - Java Web 应用 - Confluence - 漏洞汇总
CVE-2019-3395 Date: -- 类型: SSRF 影响范围: Confluence 1.*.*.*.*.3.*.*.4.*.*.5.*.* Confluence 6.0.*.1.*.6. ...