java可以搭建im即时通讯吗?
是的,Java可以用来构建即时通讯(IM)应用程序。Java是一种流行的编程语言,拥有许多开发框架和库,可以帮助开发者快速构建IM应用程序。
10年积累的成都网站建设、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先建设网站后付款的网站建设流程,更有睢宁县免费网站建设让你可以放心的选择与我们合作。
一些Java库和框架可以用于构建IM应用程序,例如:
Netty:一个异步事件驱动的网络应用程序框架,可以用于构建高性能的IM服务器。
Smack:一个用于XMPP协议的Java库,可以用于构建XMPP即时通讯客户端。
Openfire:一个用于XMPP协议的即时通讯服务器,可以与Smack一起使用以构建IM应用程序。
Apache MINA:一个灵活且可扩展的网络应用程序框架,可用于构建各种类型的网络应用程序,包括IM应用程序。
总之,Java提供了丰富的开发工具和库,可以帮助开发人员构建功能强大的即时通讯应用程序。
Java语言写段简单,但又有技术含量的即时通讯代码,不胜感激之情溢于满天下
这里有一个简单的模拟通讯 要先运行服务器端 再运行客户端 否则会报错:
服务器端代码:
package com.test3;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Server2 extends JFrame implements ActionListener , KeyListener {
JTextArea jta=null;
JScrollPane jsp=null;
JTextField jtf=null;
JButton jb=null;
JPanel jp=null;
InputStreamReader isr=null;
BufferedReader br=null;
PrintWriter pw=null;
Socket s;
String jtatext="";
public static void main(String[] args) {
// TODO Auto-generated method stub
Server2 sv2=new Server2();
}
public Server2(){
jta=new JTextArea();
jta.setEditable(false);
jsp=new JScrollPane(jta);
jtf=new JTextField(10);
jtf.addKeyListener(this);
jb=new JButton("发送");
jb.addActionListener(this);
jp=new JPanel();
jp.add(jtf);
jp.add(jb);
this.add(jsp,"Center");
this.add(jp,"South");
this.setSize(300,300);
this.setLocationRelativeTo(this);
this.setTitle("服务器端");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try{
ServerSocket ss=new ServerSocket(9999);
s=ss.accept();
isr=new InputStreamReader(s.getInputStream());
br=new BufferedReader(isr);
pw=new PrintWriter(s.getOutputStream(),true);
while(true){
String info=br.readLine();
jta.append("客户端对服务器说: "+info+"\r\n");
// this.jta.setText(jtatext);
}
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb){
try {
pw.println(jtf.getText());
jta.append("服务器对客户端说: "+jtf.getText()+"\r\n");
// jta.setText(jtatext);
jtf.setText("");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
try {
pw.println(jtf.getText());
jta.append("服务器对客户端说: "+jtf.getText()+"\r\n");
jtf.setText("");
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
@Override
public void keyReleased(KeyEvent e) {}
}
客户端代码:
package com.test3;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Client2 extends JFrame implements ActionListener ,KeyListener {
JTextArea jta=null;
JScrollPane jsp=null;
JTextField jtf=null;
JButton jb=null;
JPanel jp=null;
String jtatext="";
Socket s;
PrintWriter pw=null;
InputStreamReader isr=null;
BufferedReader br=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Client2 sv2=new Client2();
}
public Client2(){
jta=new JTextArea();
jta.setEditable(false);
jsp=new JScrollPane(jta);
jtf=new JTextField(10);
jtf.addKeyListener(this);
jb=new JButton("发送");
jb.addActionListener(this);
jp=new JPanel();
jp.add(jtf);
jp.add(jb);
this.add(jsp,"Center");
this.add(jp,"South");
this.setSize(300,300);
this.setLocationRelativeTo(this);
this.setTitle("客户端");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try {
s=new Socket("127.3.3.3",9999);
isr=new InputStreamReader(s.getInputStream());
br=new BufferedReader(isr);
pw=new PrintWriter(s.getOutputStream(),true);
while(true){
String info=br.readLine();
jta.append("服务器对客户端说: "+info+"\r\n");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb){
try {
pw.println(this.jtf.getText());
jta.append("客户端对服务器说: "+jtf.getText()+"\r\n");
jtf.setText("");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
try {
pw.println(this.jtf.getText());
jta.append("客户端对服务器说: "+jtf.getText()+"\r\n");
jtf.setText("");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void keyReleased(KeyEvent e) {}
}
怎样用java web和websocket实现网页即时通讯
服务稳定的推送服务,前段时间研究了一下goeasy,后台推送只需要两行代码, js前端推送也只需要3,4行,而且文档齐全,还提供了后台查询信息收发情况,所以我觉得GoEasy推送服务是个不错的选择。
快速入门:
GoEasy web实时推送官网:
1. 引入goeasy.js
2. 客户端订阅,
Var goeasy = new GoEasy({appkey:’your appkey’});
goeasy.subscribe(channel:”your channel”, onMessage:function(message){alert(‘received message’+ message.content)})
3. 三种推送方式
Javascript: goeasy.publish({channel:’your channel’, message:’your publish msg’});
Java SDK: GoEasy goeasy = new GoEasy(“appkey”); goeasy.publish(“your channel”,”your msg”);
RestAPI:
三步轻松实现web推送及接收
求java即时通讯的一个简单功能代码
20分!!!??(⊙o⊙)
给你这个做参考吧。自己改一下就行了。(共两个文件)
//ChatClient.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
Thread tRecv = new Thread(new RecvThread());
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(400, 300);
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();
tRecv.start();
}
public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connected!");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
/*
try {
bConnected = false;
tRecv.join();
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
*/
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText("");
try {
//System.out.println(s);
dos.writeUTF(str);
dos.flush();
//dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable {
public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
//System.out.println(str);
taContent.setText(taContent.getText() + str + '\n');
}
} catch (SocketException e) {
System.out.println("退出了,bye!");
} catch (EOFException e) {
System.out.println("推出了,bye - bye!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//ChatServer.java
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
boolean started = false;
ServerSocket ss = null;
ListClient clients = new ArrayListClient();
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
clients.add(c);
//dis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
System.out.println("对方退出了!我从List里面去掉了!");
//e.printStackTrace();
}
}
public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
System.out.println(str);
for(int i=0; iclients.size(); i++) {
Client c = clients.get(i);
c.send(str);
//System.out.println(" a string send !");
}
/*
for(IteratorClient it = clients.iterator(); it.hasNext(); ) {
Client c = it.next();
c.send(str);
}
*/
/*
IteratorClient it = clients.iterator();
while(it.hasNext()) {
Client c = it.next();
c.send(str);
}
*/
}
} catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
//s = null;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
标题名称:java即时通讯代码下载的简单介绍
当前链接:http://lswzjz.com/article/dohdoje.html