java怎么做websocket
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java怎么做websocket相关的知识,希望对你有一定的参考价值。
package com.bpms.interfaces.communicate.paycode;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import com.unify.cc.common.CommonUtil;
public class SocketSend
private String host = "11.1.66.193";
private int port = 2345;
private Socket client;
private SocketAddress remoteAddr;
private DataOutputStream os;
private DataInputStream is;
private int overTime = 0;
private int maxChang = 256;
private String error = "";
/**
* 只配服务端的地址和端口
* @param host 服务端IP
* @param port 服务端端口
*/
public SocketSend(String host , int port)
this.host = host;
this.port = port;
/**
* 设置连接报文长度的配置
* @param host IP
* @param port 端口
* @param maxChang 指定接收报文长度
*/
public SocketSend(String host , int port , int maxChang)
this.host = host;
this.port = port;
if(maxChang != 0)
this.maxChang = maxChang;
/**
* 设置连接连接超时的配置
* @param host IP
* @param port 端口
* @param maxChang 指定接收报文长度
* @param overTime 连接超时时间
*/
public SocketSend(String host , int port , int maxChang , int overTime)
this.host = host;
this.port = port;
if(maxChang != 0)
this.maxChang = maxChang;
if(overTime != 0)
this.overTime = overTime;
/**
* 初始化参数
* @throws IOException
*/
public void init() throws IOException
if(client == null)
client = new Socket();
remoteAddr = new InetSocketAddress(host,port); //创建客户端连接地址
try
if(overTime != 0)
client.connect(remoteAddr,overTime);
else
client.connect(remoteAddr);
catch (ConnectException e)
error = "SocketSend - init ConnectionServicesException";
return;
is = new DataInputStream(client.getInputStream()); //获得服务端输出流
os = new DataOutputStream(client.getOutputStream()); //获得服务端输入流
/**
* 关闭连接
* @throws IOException
*/
private void close() throws IOException
if (os != null)
os.close();
if(is != null)
is.close();
if(client != null)
client.close(); //关闭连接器
/**
* 写入流
*/
public void writeMessage(byte[] byt)
try
os.write(byt); //在服务端写入报文
os.flush();
catch (IOException e)
CommonUtil.debug("SocketSend - writeMessage 报文写入异常");
e.printStackTrace();
/**
* @param message 报文
* @return 返回报文
* @throws IOException , Exception
*/
public byte[] execute(byte[] byt) throws Exception
init();
byte[] buf = null;
//连接异常不做处理
if("".equals(error))
writeMessage(byt);
buf = new byte[maxChang];
if (is != null)
is.read(buf);//读入数据到缓冲区
close();
else
close();
throw new Exception(error);
return buf;
参考技术A 我不知道你想用websocket实现什么样的功能,一般配合HTML5的话websocket可以解决服务端和客户端消息实时传递的问题,如消息推送(webQQ这种)。它的初始连接是http转成websocket连接。
实现的话要做两件事,一端是在服务器实现service,这个有点像tcplistener一样;
另一个是客户端(浏览器)开发,可以用JS实现:
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript">
var host = '127.0.0.1';
var port = 8080;
var url = 'ws://'+host+':'+port+'/';
var w = new WebSocket(url); //构建于服务器之间的连接通信
<!--var audioElement = document.createElement('audio'); -->
<!--audioElement.setAttribute('src', 'qqmsg.mp3');-->
w.onopen = function()//通过onopen事件句柄来监听socket的打开事件
$('chat-box').innerHTML = '已连接到服务器......<br/>';
w.onmessage = function(e)//用onmessage事件句柄接受服务器传过来的数据
var msg = e.data;
var chatBox = $('chat-box');
// audioElement.play();
chatBox.innerHTML = chatBox.innerHTML+msg+'<br/>';
function send()//使用send方法向服务器发送数据
var talk = $('talk');
var nike = $('nike');
w.send('<strong style="color:red">'+nike.value+':</strong>'+talk.value);
function $(id)
return document.getElementById(id);
</script>
</head>
<body>
<div id="chat-box" style="border:1px solid #cccccc; width:400px; height:400px; overflow:scroll;"></div>
昵称:
<input type="text" id="nike"/>
<br/>
内容:
<input type="text" id="talk"/>
<input type="button" id="send" onClick="send();" value="发送"/>
</body>
可参考IBM社区 http://www.ibm.com/developerworks/cn/web/1112_huangxa_websocket/
</html> 参考技术B websocket需要服务端程序支持,比如最新的Tomcat 参考技术C 貌似有框架可以哦 参考技术D 你在java的API里找找相应的类,里边有介绍
以上是关于java怎么做websocket的主要内容,如果未能解决你的问题,请参考以下文章