java聊天功能怎么做?

Python015

java聊天功能怎么做?,第1张

想要实现java聊天功能比较简单,要么直接找源码,要么使用第三方的sdk做一些开发。建议可以考虑接入ZEGO即时通讯SDK来实现,支持Android java开发,集成方便,一对一、一对多聊天都可快速搭建,重要的是不担心消息会丢失,千万级并发也稳定,个人建议你们可以试试。

/**

* 基于UDP协议的聊天程序

*

*2007.9.18

* */

//导入包

import java.awt.*

import java.awt.event.*

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.*

import java.net.*

public class Chat extends JFrame implements ActionListener

{

//广播地址或者对方的地址

public static final String sendIP = "172.18.8.255"

//发送端口9527

public static final int sendPort = 9527

JPanel p = new JPanel()

List lst = new List() //消息显示

JTextField txtIP = new JTextField(18)//填写IP地址

JTextField txtMSG = new JTextField(20)//填写发送消息

JLabel lblIP = new JLabel("IP地址:")

JLabel lblMSG = new JLabel("消息:")

JButton btnSend = new JButton("发送")

byte [] buf

//定义DatagramSocket的对象必须进行异常处理

//发送和接收数据报包的套接字

DatagramSocket ds = null

//=============构造函数=====================

public Chat()

{

CreateInterFace()

//注册消息框监听器

txtMSG.addActionListener(this)

btnSend.addActionListener(this)

try

{

//端口:9527

ds =new DatagramSocket(sendPort)

}

catch(Exception ex)

{

ex.printStackTrace()

}

//============接受消息============

//匿名类

new Thread(new Runnable()

{

public void run()

{

byte buf[] = new byte[1024]

//表示接受数据报包

while(true)

{

try

{

DatagramPacket dp = new DatagramPacket(buf,1024,InetAddress.getByName(txtIP.getText()),sendPort)

ds.receive(dp)

lst.add("【消息来自】◆" + dp.getAddress().getHostAddress() + "◆"+"【说】:" + new String (buf,0,dp.getLength()) /*+ dp.getPort()*/,0)

}

catch(Exception e)

{

if(ds.isClosed())

{

e.printStackTrace()

}

}

}

}

}).start()

//关闭窗体事件

this.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent w)

{

System.out.println("test")

int n=JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION)

if(n==JOptionPane.YES_OPTION)

{

dispose()

System.exit(0)

ds.close()//关闭ds对象//关闭数据报套接字

}

}

})

}

//界面设计布局

public void CreateInterFace()

{

this.add(lst,BorderLayout.CENTER)

this.add(p,BorderLayout.SOUTH)

p.add(lblIP)

p.add(txtIP)

p.add(lblMSG)

p.add(txtMSG)

p.add(btnSend)

txtIP.setText(sendIP)

//背景颜色

lst.setBackground(Color.yellow)

//JAVA默认风格

this.setUndecorated(true)

this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME)

this.setSize(600,500)

this.setTitle("〓聊天室〓")

this.setResizable(false)//不能改变窗体大小

this.setLocationRelativeTo(null)//窗体居中

this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)

this.setVisible(true)

txtMSG.requestFocus()//消息框得到焦点

}

//===============================Main函数===============================

public static void main(String[]args)

{

new Chat()

}

//================================发送消息===============================

//消息框回车发送消息事件

public void actionPerformed(ActionEvent e)

{

//得到文本内容

buf = txtMSG.getText().getBytes()

//判断消息框是否为空

if (txtMSG.getText().length()==0)

{

JOptionPane.showMessageDialog(null,"发送消息不能为空","提示",JOptionPane.WARNING_MESSAGE)

}

else{

try

{

InetAddress address = InetAddress.getByName(sendIP)

DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort)

ds.send(dp)

}

catch(Exception ex)

{

ex.printStackTrace()

}

}

txtMSG.setText("")//清空消息框

//点发送按钮发送消息事件

if(e.getSource()==btnSend)

{

buf = txtMSG.getText().getBytes()

try

{

DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort)

}

catch(Exception ex)

{

ex.printStackTrace()

}

txtMSG.setText("")//清空消息框

txtMSG.requestFocus()

}

}

}

聊天具体是这么聊,聊天室、群聊、一对一。java其实websocket足矣,ws服务器部署起来,客户端程序连接ws服务器服务,用服务器转发作为消息传输机制,当然你要大文件传输和视频通话就用socket转发连接,p2p通信,这里的p2pjava因为都是服务器部署,不需要做外网打洞穿透,因为web服务器他是本地环境,明白不。你用windows应用程序就需要外网穿透打洞,你不可能视频数据也让服务器转发吧,聊天人数越来越多,服务器承受不了,它需要服务器指派点对点单向传输,不去途径服务器。