A. 求網路聊天室源碼,用JSP或Servlet都行
用ajax和jsp寫的,可以實現無刷新頁面顯示聊天記錄js/js.js文件varnbsp;obj;varnbsp;getId=document.getElementById;functionnbsp;objXMLHttp(){nbsp;nbsp;nbsp;nbsp;varnbsp;XmlHttpObjnbsp;=nbsp;false;nbsp;nbsp;nbsp;nbsp;if(window.ActiveXObject)nbsp;nbsp;nbsp;nbsp;{nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;XmlHttpObjnbsp;=nbsp;newnbsp;ActiveXObject(「MSXML2.XMLHTTP.3.0「);nbsp;nbsp;nbsp;nbsp;}nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;elsenbsp;ifnbsp;(window.XMLHttpRequest)nbsp;nbsp;nbsp;nbsp;{nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;XmlHttpObjnbsp;=nbsp;newnbsp;XMLHttpRequest();nbsp;nbsp;nbsp;nbsp;}nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;returnnbsp;XmlHttpObj;}functionnbsp;sendMsg(auto){nbsp;obj=objXMLHttp();nbsp;if(!auto)//nbsp;{nbsp;nbsp;varnbsp;myContentnbsp;=nbsp;getId(「myText「).value;nbsp;nbsp;if(myContentnbsp;!=nbsp;「「)nbsp;nbsp;{nbsp;nbsp;nbsp;if(obj)nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;{nbsp;nbsp;nbsp;nbsp;obj.onreadystatechangenbsp;=nbsp;addChat;nbsp;nbsp;nbsp;nbsp;obj.open(「POST「,「do.jsp「,nbsp;true);nbsp;nbsp;nbsp;nbsp;obj.setRequestHeader(「Content-Type「,「application/x-www-form-urlencoded;「);nbsp;nbsp;nbsp;nbsp;nbsp;obj.send(「myContent=「nbsp;+nbsp;myContent);nbsp;nbsp;nbsp;nbsp;getId(「myText「).value=「「;nbsp;nbsp;nbsp;nbsp;getId(「myText「).focus();nbsp;nbsp;nbsp;nbsp;Scroll();nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;}nbsp;nbsp;}nbsp;nbsp;elsenbsp;nbsp;{nbsp;nbsp;nbsp;alert(「nbsp;(*^__^*)nbsp;你不告訴我們,我們怎麼知道你們保持沉默呢「);nbsp;nbsp;nbsp;getId(「myText「).value=「保持沉默「;nbsp;nbsp;nbsp;getId(「myText「).focus();nbsp;nbsp;}nbsp;}nbsp;else//nbsp;{nbsp;nbsp;if(obj)nbsp;nbsp;{nbsp;nbsp;nbsp;obj.onreadystatechangenbsp;=nbsp;addChat;nbsp;nbsp;nbsp;obj.open(「POST「,「do.jsp「,nbsp;true);nbsp;nbsp;nbsp;obj.setRequestHeader(「Content-Type「,「application/x-www-form-urlencoded;「);nbsp;nbsp;nbsp;nbsp;obj.send(null);nbsp;nbsp;nbsp;Scroll();nbsp;nbsp;}nbsp;}}functionnbsp;addChat()//添加聊天記錄{nbsp;varnbsp;docnbsp;=nbsp;「「;nbsp;if(obj.readyStatenbsp;==nbsp;4nbsp;amp;amp;nbsp;obj.statusnbsp;==nbsp;200)nbsp;{nbsp;nbsp;doc=obj.responseText;nbsp;nbsp;if(docnbsp;!=nbsp;「「)nbsp;nbsp;{nbsp;nbsp;nbsp;getId(「says「).value=doc;nbsp;nbsp;}nbsp;}}functionnbsp;getChatContent(){nbsp;tnbsp;=nbsp;setInterval(『sendMsg(true)『,800);}functionnbsp;quickSend(){nbsp;nbsp;if(getId(「sendType「).value==「ctrlEnter「)nbsp;{nbsp;nbsp;if(window.event.ctrlKeyamp;amp;window.event.keyCode==13)nbsp;nbsp;{nbsp;nbsp;nbsp;sendMsg(false);nbsp;nbsp;}nbsp;}nbsp;elsenbsp;if(getId(「sendType「).value==「enterKey「)nbsp;{nbsp;nbsp;if(window.event.keyCode==13)nbsp;nbsp;{nbsp;nbsp;nbsp;sendMsg(false);nbsp;nbsp;}nbsp;}}functionnbsp;loginConfirm(){nbsp;if(window.event.keyCode==13)nbsp;{nbsp;nbsp;checkUser();nbsp;}}functionnbsp;checkUser(){nbsp;if(getId(「userName「).value==nullnbsp;||nbsp;getId(「userName「).value=
B. 速求用java語言寫聊天室的源代碼
【ClientSocketDemo.java 客戶端Java源代碼】
import java.net.*;
import java.io.*;
public class ClientSocketDemo
{
//聲明客戶端Socket對象socket
Socket socket = null;
//聲明客戶器端數據輸入輸出流
DataInputStream in;
DataOutputStream out;
//聲明字元串數組對象response,用於存儲從伺服器接收到的信息
String response[];
//執行過程中,沒有參數時的構造方法,本地伺服器在本地,取默認埠10745
public ClientSocketDemo()
{
try
{
//創建客戶端socket,伺服器地址取本地,埠號為10745
socket = new Socket("localhost",10745);
//創建客戶端數據輸入輸出流,用於對伺服器端發送或接收數據
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//獲取客戶端地址及埠號
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
//向伺服器發送數據
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
//從伺服器接收數據
response = new String[3];
for (int i = 0; i < response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
//執行過程中,有一個參數時的構造方法,參數指定伺服器地址,取默認埠10745
public ClientSocketDemo(String hostname)
{
try
{
//創建客戶端socket,hostname參數指定伺服器地址,埠號為10745
socket = new Socket(hostname,10745);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
response = new String[3];
for (int i = 0; i < response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
//執行過程中,有兩個個參數時的構造方法,第一個參數hostname指定伺服器地址
//第一個參數serverPort指定伺服器埠號
public ClientSocketDemo(String hostname,String serverPort)
{
try
{
socket = new Socket(hostname,Integer.parseInt(serverPort));
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
response = new String[3];
for (int i = 0; i < response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
public static void main(String[] args)
{
String comd[] = args;
if(comd.length == 0)
{
System.out.println("Use localhost(127.0.0.1) and default port");
ClientSocketDemo demo = new ClientSocketDemo();
}
else if(comd.length == 1)
{
System.out.println("Use default port");
ClientSocketDemo demo = new ClientSocketDemo(args[0]);
}
else if(comd.length == 2)
{
System.out.println("Hostname and port are named by user");
ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);
}
else System.out.println("ERROR");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
【ServerSocketDemo.java 伺服器端Java源代碼】
import java.net.*;
import java.io.*;
public class ServerSocketDemo
{
//聲明ServerSocket類對象
ServerSocket serverSocket;
//聲明並初始化伺服器端監聽埠號常量
public static final int PORT = 10745;
//聲明伺服器端數據輸入輸出流
DataInputStream in;
DataOutputStream out;
//聲明InetAddress類對象ip,用於獲取伺服器地址及埠號等信息
InetAddress ip = null;
//聲明字元串數組對象request,用於存儲從客戶端發送來的信息
String request[];
public ServerSocketDemo()
{
request = new String[3]; //初始化字元串數組
try
{
//獲取本地伺服器地址信息
ip = InetAddress.getLocalHost();
//以PORT為服務埠號,創建serverSocket對象以監聽該埠上的連接
serverSocket = new ServerSocket(PORT);
//創建Socket類的對象socket,用於保存連接到伺服器的客戶端socket對象
Socket socket = serverSocket.accept();
System.out.println("This is server:"+String.valueOf(ip)+PORT);
//創建伺服器端數據輸入輸出流,用於對客戶端接收或發送數據
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//接收客戶端發送來的數據信息,並顯示
request[0] = in.readUTF();
request[1] = in.readUTF();
request[2] = in.readUTF();
System.out.println("Received messages form client is:");
System.out.println(request[0]);
System.out.println(request[1]);
System.out.println(request[2]);
//向客戶端發送數據
out.writeUTF("Hello client!");
out.writeUTF("Your ip is:"+request[1]);
out.writeUTF("Your port is:"+request[2]);
}
catch(IOException e){e.printStackTrace();}
}
public static void main(String[] args)
{
ServerSocketDemo demo = new ServerSocketDemo();
}
}
C. 快速搭建一個匿名聊天室
大家好,我將介紹一個基於livekit 和 Next.js 構建的匿名聊天室 —— anonymous-chat-room。以下為搭建步驟:
請訪問GitHub頁面獲取源碼:GitHub地址
在線體驗請移步到此網址:在線體驗地址
本地部署步驟如下:
首先,將項目克隆或下載到本地計算機。
然後,轉至項目所在目錄。
執行命令安裝項目依賴。
接著,在 cloud.livekit.io 創建一個新的 Project,並獲取 apikey,通過項目設置界面獲取。
根據提示,在env.example文件中修改環境變數,並將該文件重命名為 env.local。
最後,運行以下命令:
在瀏覽器中輸入"http://localhost:3000",即可打開匿名聊天室。
此開源項目已收錄至GitHub,歡迎點擊star以示支持。
您的點贊、收藏、轉發和評論,都是對作者的鼓勵。如您覺得此文章對您有所幫助,還請分享給更多需要的人,謝謝!