java即時(shí)通訊代碼下載的簡(jiǎn)單介紹

java可以搭建im即時(shí)通訊嗎?

是的,Java可以用來(lái)構(gòu)建即時(shí)通訊(IM)應(yīng)用程序。Java是一種流行的編程語(yǔ)言,擁有許多開(kāi)發(fā)框架和庫(kù),可以幫助開(kāi)發(fā)者快速構(gòu)建IM應(yīng)用程序。

10年積累的成都網(wǎng)站建設(shè)、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有睢寧縣免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

一些Java庫(kù)和框架可以用于構(gòu)建IM應(yīng)用程序,例如:

Netty:一個(gè)異步事件驅(qū)動(dòng)的網(wǎng)絡(luò)應(yīng)用程序框架,可以用于構(gòu)建高性能的IM服務(wù)器。

Smack:一個(gè)用于XMPP協(xié)議的Java庫(kù),可以用于構(gòu)建XMPP即時(shí)通訊客戶(hù)端。

Openfire:一個(gè)用于XMPP協(xié)議的即時(shí)通訊服務(wù)器,可以與Smack一起使用以構(gòu)建IM應(yīng)用程序。

Apache MINA:一個(gè)靈活且可擴(kuò)展的網(wǎng)絡(luò)應(yīng)用程序框架,可用于構(gòu)建各種類(lèi)型的網(wǎng)絡(luò)應(yīng)用程序,包括IM應(yīng)用程序。

總之,Java提供了豐富的開(kāi)發(fā)工具和庫(kù),可以幫助開(kāi)發(fā)人員構(gòu)建功能強(qiáng)大的即時(shí)通訊應(yīng)用程序。

Java語(yǔ)言寫(xiě)段簡(jiǎn)單,但又有技術(shù)含量的即時(shí)通訊代碼,不勝感激之情溢于滿(mǎn)天下

這里有一個(gè)簡(jiǎn)單的模擬通訊 要先運(yùn)行服務(wù)器端 再運(yùn)行客戶(hù)端 否則會(huì)報(bào)錯(cuò):

服務(wù)器端代碼:

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("發(fā)送");

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("服務(wù)器端");

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("客戶(hù)端對(duì)服務(wù)器說(shuō):???"+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("服務(wù)器對(duì)客戶(hù)端說(shuō):???"+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("服務(wù)器對(duì)客戶(hù)端說(shuō):???"+jtf.getText()+"\r\n");

jtf.setText("");

}?catch?(Exception?e1)?{

e1.printStackTrace();

}

}

}

@Override

public?void?keyReleased(KeyEvent?e)?{}

}

客戶(hù)端代碼:

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("發(fā)送");

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("客戶(hù)端");

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("服務(wù)器對(duì)客戶(hù)端說(shuō):???"+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("客戶(hù)端對(duì)服務(wù)器說(shuō):???"+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("客戶(hù)端對(duì)服務(wù)器說(shuō):???"+jtf.getText()+"\r\n");

jtf.setText("");

}?catch?(Exception?e1)?{

//?TODO?Auto-generated?catch?block

e1.printStackTrace();

}

}

}

public?void?keyReleased(KeyEvent?e)?{}

}

怎樣用java web和websocket實(shí)現(xiàn)網(wǎng)頁(yè)即時(shí)通訊

服務(wù)穩(wěn)定的推送服務(wù),前段時(shí)間研究了一下goeasy,后臺(tái)推送只需要兩行代碼, js前端推送也只需要3,4行,而且文檔齊全,還提供了后臺(tái)查詢(xún)信息收發(fā)情況,所以我覺(jué)得GoEasy推送服務(wù)是個(gè)不錯(cuò)的選擇。

快速入門(mén):

GoEasy web實(shí)時(shí)推送官網(wǎng):

1. 引入goeasy.js

2. 客戶(hù)端訂閱,

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:

三步輕松實(shí)現(xiàn)web推送及接收

求java即時(shí)通訊的一個(gè)簡(jiǎn)單功能代碼

20分!?。???(⊙o⊙)

給你這個(gè)做參考吧。自己改一下就行了。(共兩個(gè)文件)

//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("請(qǐng)關(guān)掉相關(guān)程序并重新運(yùn)行服務(wù)器!");

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("對(duì)方退出了!我從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();

}

}

}

}

}

網(wǎng)頁(yè)名稱(chēng):java即時(shí)通訊代碼下載的簡(jiǎn)單介紹
轉(zhuǎn)載源于:http://muchs.cn/article24/dohdoje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、定制開(kāi)發(fā)、網(wǎng)站營(yíng)銷(xiāo)、做網(wǎng)站響應(yīng)式網(wǎng)站、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作