Skip to content
Sign upLog in
← Back to Community

Socket.io tutorial

Profile icon
Bunnytoes

Socket.io tutorial

In this tutorial you will learn socket.io java and how to make a chatroom
step 1: make a file called whatever you want for this example we will call it GroupChat.java, add a public class called whatever you named the file without the .java, add add these imports

import java.net.*; import java.io.*; import java.util.*;

then add

private static final String TERMINATE = "!exit"; public static String name; static volatile boolean finished = false; static boolean banned = false;

in the public class
then add a main method

public static void main(String[] args) { }

step 2: make a main.sh file and a .replit, in the .replit write
run = "bash main.sh"
and in the bash file write

read -p "enter multicast host " room read -p "enter port number " port java GroupChat $room $port

replace GroupChat with whatever the public class is, this code will run it so you don't have to go to shell, then add

if (args.length != 2) System.out.println("Two arguments required: <multicast-host> <port-number>");

for if they don't enter two values in the bash file
then add a else-try-catch in main method

else{ try { } catch(SocketException se) { System.out.println("Error creating socket"); se.printStackTrace(); } catch(IOException ie) { System.out.println("Error reading/writing from/to socket"); ie.printStackTrace(); } }

those are the exceptions you have to catch, then add an InetAdress to the beginning of the try block

InetAddress group = InetAddress.getByName(args[0]);

this will take the first value you entered on the bash file when it runs, then make an int called port, whcih is the second variable you enter when the bash file runs write after the InetAddress

int port = Integer.parseInt(args[1]);

then add this after it

name = System.getenv("REPL_OWNER");

then add

// we will make these later name = Filter(name,Swears.wordsToRemove); banFilter(name,banList); if (banned == true) { return; } ArrayList<String> peopleOn = new ArrayList<>();

to set the name to the repl username
then add this

String stuff = name + " joined"; peopleOn.add(name);

then, to send it frist add

byte[] buff = stuff.getBytes(); DatagramPacket data = new DatagramPacket(buff,buff.length,group,port);

then make a MulticastSocket

MulticastSocket socket = new MulticastSocket(port);

then add

socket.send(data); socket.setTimeToLive(0); socket.joinGroup(group);

then exit the public class and add all this

class ReadThread implements Runnable { private MulticastSocket socket; private InetAddress group; private int port; private static final int MAX_LEN = 1000; ReadThread(MulticastSocket socket,InetAddress group,int port) { this.socket = socket; this.group = group; this.port = port; } @Override public void run() { //this is for when we make an exit while(!GroupChat.finished) { byte[] buffer = new byte[ReadThread.MAX_LEN]; DatagramPacket datagram = new DatagramPacket(buffer,buffer.length,group,port); String message; try { socket.receive(datagram); message = new String(buffer,0,datagram.getLength(),"UTF-8"); if(!message.startsWith(GroupChat.name)) System.out.println(message); } catch(IOException e) { System.out.println("You have left the chat"); } } } }

then add where we were in the public class, add

ReadThread(socket,group,port)); t.start(); System.out.println("\nPeople on " + peopleOn); System.out.println("Welcome to the chat, " + name + "\n");

now for the main body of the chat, add

while(true) { String message; message = sc.nextLine(); message = Filter(message, Swears.wordsToRemove); if (message.equalsIgnoreCase("!b")) { message = (char)27 + "[1m" + message; } if (message.equalsIgnoreCase("!s")) { message = (char)27 + "[9m" + message; } if (message.equalsIgnoreCase("!r")) { message = (char)27 + "[0m" + message; message = (char)27 + "[39m" + message; message = (char)27 + "[49m" + message; } if (message.equalsIgnoreCase("!i")) { message = (char)27 + "[3m" + message; } if (message.equalsIgnoreCase("!u")) { message = (char)27 + "[4m" + message; } if (message.equalsIgnoreCase("!red")) { message = (char)27 + "[31m" + message; } if (message.equalsIgnoreCase("!yellow")) { message = (char)27 + "[33m" + message; } if (message.equalsIgnoreCase("!green")) { message = (char)27 + "[32m" + message; } if (message.equalsIgnoreCase("!blue")) { message = (char)27 + "[34m" + message; } if (message.equalsIgnoreCase("!cyan")) { message = (char)27 + "[36m" + message; } if (message.equalsIgnoreCase("!magenta")) { message = (char)27 + "[35m" + message; } if (message.equalsIgnoreCase("!black")) { message = (char)27 + "[30m" + message; } if (message.equalsIgnoreCase("!redbg")) { message = (char)27 + "[41m" + message; } if (message.equalsIgnoreCase("!yellowbg")) { message = (char)27 + "[43m" + message; } if (message.equalsIgnoreCase("!greenbg")) { message = (char)27 + "[42m" + message; } if (message.equalsIgnoreCase("!bluebg")) { message = (char)27 + "[44m" + message; } if (message.equalsIgnoreCase("!cyanbg")) { message = (char)27 + "[46m" + message; } if (message.equalsIgnoreCase("!magentabg")) { message = (char)27 + "[45m" + message; } if (message.equalsIgnoreCase("!blackbg")) { message = (char)27 + "[40m" + message; } if (message.equalsIgnoreCase("!peopleon")) { System.out.println("People on " + peopleOn); } if (message.equalsIgnoreCase("!ascii")) { AsciiArt.toAscii(message); } if(message.equalsIgnoreCase(GroupChat.TERMINATE)) { peopleOn.remove(name); finished = true; socket.leaveGroup(group); socket.close(); break; } message = name + ": " + message; byte[] buffer = message.getBytes(); DatagramPacket datagram = new DatagramPacket(buffer,buffer.length,group,port); socket.send(datagram); }

the ! things are for commands, I use them to change colors, leave, and other stuff and now for the filter and ban Filter add

public static String Filter(String message, String[] wordsToRemove) { for (String s : wordsToRemove) { if (message.contains(s)) { for (char c : message.toCharArray()) { if (c == ' ') { continue; } message = message.replace(c, '*'); } } } return message; } public static void banFilter(String name, String[] banList) { for (String s : banList) { if (name.equals(s)) { System.out.println("You are banned"); finished = true; banned = true; return; } } }

to make the swear filter work add a file calle Swears.java, add

public class Swears { }

and add an array called wordsToRemove

static String[] wordsToRemove = {};

add what words you want to filter in "" seperate with a , and for the ban list add the username of who you want to ban
add a file called AsciiArt.java and add

public class AsciiArt{ public static String toAscii (String message) { if (message.contains("A")) { String TheLetter = " ** \n" + " **** \n" + " **//** \n" + " ** //** \n" + " **********\n" + "/**//////**\n" + "/** /**\n" + "// // "; message = message.replace("A",TheLetter); } if (message.contains("B")) { String TheLetter = " ****** \n" + "/*////** \n" + "/* /** \n" + "/****** \n" + "/*//// **\n" + "/* /**\n" + "/******* \n" + "/////// "; message = message.replace("B",TheLetter); } if (message.contains("C")) { String TheLetter = " ****** \n" + " **////**\n" + " ** // \n" + "/** \n" + "/** \n" + "//** **\n" + " //****** \n" + " ////// "; message = message.replace("C",TheLetter); } if (message.contains("D")) { String TheLetter = " ******* \n" + "/**////** \n" + "/** /**\n" + "/** /**\n" + "/** /**\n" + "/** ** \n" + "/******* \n" + "/////// "; message = message.replace("D",TheLetter); } if (message.contains("E")) { String TheLetter = " ********\n" + "/**///// \n" + "/** \n" + "/******* \n" + "/**//// \n" + "/** \n" + "/********\n" + "//////// "; message = message.replace("E",TheLetter); }if (message.contains("F")) { String TheLetter = " ********\n" + "/**///// \n" + "/** \n" + "/******* \n" + "/**//// \n" + "/** \n" + "/** \n" + "// "; message = message.replace("F",TheLetter); }if (message.contains("G")) { String TheLetter = " ******** \n" + " **//////**\n" + " ** // \n" + "/** \n" + "/** *****\n" + "//** ////**\n" + " //******** \n" + " //////// "; message = message.replace("G",TheLetter); }if (message.contains("H")) { String TheLetter = " ** **\n" + "/** /**\n" + "/** /**\n" + "/**********\n" + "/**//////**\n" + "/** /**\n" + "/** /**\n" + "// // "; message = message.replace("H",TheLetter); }if (message.contains("I")) { String TheLetter = " **\n" + "/**\n" + "/**\n" + "/**\n" + "/**\n" + "/**\n" + "/**\n" + "// "; message = message.replace("I",TheLetter); }if (message.contains("J")) { String TheLetter = " **\n" + " /**\n" + " /**\n" + " /**\n" + " /**\n" + " ** /**\n" + "//***** \n" + " ///// "; message = message.replace("J",TheLetter); }if (message.contains("K")) { String TheLetter = " ** **\n" + "/** ** \n" + "/** ** \n" + "/**** \n" + "/**/** \n" + "/**//** \n" + "/** //**\n" + "// // "; message = message.replace("K",TheLetter); }if (message.contains("L")) { String TheLetter = " ** \n" + "/** \n" + "/** \n" + "/** \n" + "/** \n" + "/** \n" + "/********\n" + "//////// "; message = message.replace("L",TheLetter); }if (message.contains("M")) { String TheLetter = " **** ****\n" + "/**/** **/**\n" + "/**//** ** /**\n" + "/** //*** /**\n" + "/** //* /**\n" + "/** / /**\n" + "/** /**\n" + "// // \n" + " ////// "; message = message.replace("M",TheLetter); }if (message.contains("N")) { String TheLetter = " **** **\n" + "/**/** /**\n" + "/**//** /**\n" + "/** //** /**\n" + "/** //**/**\n" + "/** //****\n" + "/** //***\n" + "// /// "; message = message.replace("N",TheLetter); }if (message.contains("O")) { String TheLetter = " ******* \n" + " **/////** \n" + " ** //**\n" + "/** /**\n" + "/** /**\n" + "//** ** \n" + " //******* \n" + " /////// "; message = message.replace("O",TheLetter); }if (message.contains("P")) { String TheLetter = " ******* \n" + "/**////**\n" + "/** /**\n" + "/******* \n" + "/**//// \n" + "/** \n" + "/** \n" + "// "; message = message.replace("P",TheLetter); }if (message.contains("Q")) { String TheLetter = " ******* \n" + " **/////** \n" + " ** //** \n" + "/** /** \n" + "/** **/** \n" + "//** // ** \n" + " //******* **\n" + " /////// // "; message = message.replace("Q",TheLetter); }if (message.contains("R")) { String TheLetter = " ******* \n" + "/**////** \n" + "/** /** \n" + "/******* \n" + "/**///** \n" + "/** //** \n" + "/** //**\n" + "// // "; message = message.replace("R",TheLetter); }if (message.contains("S")) { String TheLetter = " ********\n" + " **////// \n" + "/** \n" + "/*********\n" + "////////**\n" + " /**\n" + " ******** \n" + "//////// "; message = message.replace("S",TheLetter); }if (message.contains("T")) { String TheLetter = " **********\n" + "/////**/// \n" + " /** \n" + " /** \n" + " /** \n" + " /** \n" + " /** \n" + " // "; message = message.replace("T",TheLetter); }if (message.contains("U")) { String TheLetter = " ** **\n" + "/** /**\n" + "/** /**\n" + "/** /**\n" + "/** /**\n" + "/** /**\n" + "//******* \n" + " /////// "; message = message.replace("U",TheLetter); }if (message.contains("V")) { String TheLetter = " ** **\n" + "/** /**\n" + "/** /**\n" + "//** ** \n" + " //** ** \n" + " //**** \n" + " //** \n" + " // "; message = message.replace("V",TheLetter); }if (message.contains("W")) { String TheLetter = " ** **\n" + "/** /**\n" + "/** * /**\n" + "/** *** /**\n" + "/** **/**/**\n" + "/**** //****\n" + "/**/ ///**\n" + "// // "; message = message.replace("W",TheLetter); }if (message.contains("X")) { String TheLetter = " ** **\n" + "//** ** \n" + " //** ** \n" + " //*** \n" + " **/** \n" + " ** //** \n" + " ** //**\n" + "// // "; message = message.replace("X",TheLetter); }if (message.contains("Y")) { String TheLetter = " ** **\n" + "//** ** \n" + " //**** \n" + " //** \n" + " /** \n" + " /** \n" + " /** \n" + " // "; message = message.replace("Y",TheLetter); }if (message.contains("Z")) { String TheLetter = " ********\n" + "//////** \n" + " ** \n" + " ** \n" + " ** \n" + " ** \n" + " ********\n" + "//////// "; message = message.replace("Z",TheLetter); }if (message.contains("a")) { String TheLetter = " ****** \n" + " //////** \n" + " ******* \n" + " **////** \n" + "//********\n" + " //////// "; message = message.replace("a",TheLetter); }if (message.contains("b")) { String TheLetter = " ** \n" + "/** \n" + "/** \n" + "/****** \n" + "/**///**\n" + "/** /**\n" + "/****** \n" + "///// "; message = message.replace("b",TheLetter); }if (message.contains("c")) { String TheLetter = " ***** \n" + " **///**\n" + "/** // \n" + "/** **\n" + "//***** \n" + " ///// "; message = message.replace("c",TheLetter); }if (message.contains("d")) { String TheLetter = " **\n" + " /**\n" + " /**\n" + " ******\n" + " **///**\n" + "/** /**\n" + "//******\n" + " ////// "; message = message.replace("d",TheLetter); }if (message.contains("e")) { String TheLetter = " ***** \n" + " **///**\n" + "/*******\n" + "/**//// \n" + "//******\n" + " ////// "; message = message.replace("e",TheLetter); }if (message.contains("f")) { String TheLetter = " ****\n" + " /**/ \n" + " ******\n" + "///**/ \n" + " /** \n" + " /** \n" + " /** \n" + " // "; message = message.replace("f",TheLetter); }if (message.contains("g")) { String TheLetter = " ***** \n" + " **///**\n" + "/** /**\n" + "//******\n" + " /////**\n" + " ***** \n" + " ///// "; message = message.replace("g",TheLetter); }if (message.contains("h")) { String TheLetter = " ** \n" + "/** \n" + "/** \n" + "/****** \n" + "/**///**\n" + "/** /**\n" + "/** /**\n" + "// // "; message = message.replace("h",TheLetter); }if (message.contains("i")) { String TheLetter = " **\n" + "// \n" + " **\n" + "/**\n" + "/**\n" + "/**\n" + "/**\n" + "// "; message = message.replace("i",TheLetter); }if (message.contains("j")) { String TheLetter = " **\n" + " // \n" + " **\n" + " /**\n" + " /**\n" + " **/**\n" + "//*** \n" + " /// "; message = message.replace("j",TheLetter); }if (message.contains("k")) { String TheLetter = " ** \n" + "/** \n" + "/** **\n" + "/** ** \n" + "/**** \n" + "/**/** \n" + "/**//**\n" + "// // "; message = message.replace("k",TheLetter); }if (message.contains("l")) { String TheLetter = " **\n" + " /**\n" + " /**\n" + " /**\n" + " /**\n" + " /**\n" + " ***\n" + "/// \n" + " ////// "; message = message.replace("l",TheLetter); }if (message.contains("m")) { String TheLetter = " ********** \n" + "//**//**//**\n" + " /** /** /**\n" + " /** /** /**\n" + " *** /** /**\n" + "/// // // "; message = message.replace("m",TheLetter); }if (message.contains("n")) { String TheLetter = " ******* \n" + "//**///**\n" + " /** /**\n" + " /** /**\n" + " *** /**\n" + "/// // \n" + " ////// "; message = message.replace("n",TheLetter); }if (message.contains("o")) { String TheLetter = " ****** \n" + " **////**\n" + "/** /**\n" + "/** /**\n" + "//****** \n" + " ////// "; message = message.replace("o",TheLetter); }if (message.contains("p")) { String TheLetter = " ****** \n" + "/**///**\n" + "/** /**\n" + "/****** \n" + "/**/// \n" + "/** \n" + "// "; message = message.replace("p",TheLetter); }if (message.contains("q")) { String TheLetter = " **** \n" + " **//** \n" + "/** /** \n" + "//***** \n" + " ////** \n" + " /***\n" + " /// "; message = message.replace("q",TheLetter); }if (message.contains("r")) { String TheLetter = " ******\n" + "//**//*\n" + " /** / \n" + " /** \n" + "/*** \n" + "/// "; message = message.replace("r",TheLetter); } if (message.contains("s")) { String TheLetter = " ******\n" + " **//// \n" + "//***** \n" + " /////**\n" + " ****** \n" + "////// "; message = message.replace("s",TheLetter); }if (message.contains("t")) { String TheLetter = " ** \n" + " /** \n" + " ******\n" + "///**/ \n" + " /** \n" + " /** \n" + " //** \n" + " // "; message = message.replace("t",TheLetter); }if (message.contains("u")) { String TheLetter = " ** **\n" + "/** /**\n" + "/** /**\n" + "/** /**\n" + "//******\n" + " ////// "; message = message.replace("u",TheLetter); }if (message.contains("v")) { String TheLetter = " ** **\n" + "/** /**\n" + "//** /** \n" + " //**** \n" + " //** \n" + " // "; message = message.replace("v",TheLetter); }if (message.contains("w")) { String TheLetter = " *** **\n" + "//** * /**\n" + " /** ***/**\n" + " /****/****\n" + " ***/ ///**\n" + "/// /// "; message = message.replace("w",TheLetter); }if (message.contains("x")) { String TheLetter = " ** **\n" + "//** ** \n" + " //*** \n" + " **/** \n" + " ** //**\n" + "// // "; message = message.replace("x",TheLetter); }if (message.contains("y")) { String TheLetter = " ** **\n" + " //** ** \n" + " //*** \n" + " /** \n" + " ** \n" + " ** \n" + " // "; message = message.replace("y",TheLetter); }if (message.contains("z")) { String TheLetter = " ******\n" + "////** \n" + " ** \n" + " ** \n" + " ******\n" + "////// n" + " ////// "; message = message.replace("z",TheLetter); } return message; } }

full code

import java.net.*; import java.io.*; import java.util.*; public class GroupChat { private static final String TERMINATE = "!exit"; public static String name; static volatile boolean finished = false; static boolean banned = false; public static void main(String[] args) { if (args.length != 2) System.out.println("Two arguments required: <multicast-host> <port-number>"); else { try { String[] banList = {"imma-steal-ur-heart"}; InetAddress group = InetAddress.getByName(args[0]); int port = Integer.parseInt(args[1]); Scanner sc = new Scanner(System.in); name = System.getenv("REPL_OWNER"); name = Filter(name,Swears.wordsToRemove); banFilter(name,banList); if (banned == true) { return; } ArrayList<String> peopleOn = new ArrayList<>(); String stuff = name + " joined"; peopleOn.add(name); byte[] buff = stuff.getBytes(); DatagramPacket data = new DatagramPacket(buff,buff.length,group,port); MulticastSocket socket = new MulticastSocket(port); socket.send(data); socket.setTimeToLive(0); socket.joinGroup(group); Thread t = new Thread(new ReadThread(socket,group,port)); t.start(); System.out.println("\nPeople on " + peopleOn); System.out.println("Welcome to the chat, " + name + "\n"); while(true) { String message; message = sc.nextLine(); message = Filter(message, Swears.wordsToRemove); if (message.equalsIgnoreCase("!b")) { message = (char)27 + "[1m" + message; } if (message.equalsIgnoreCase("!s")) { message = (char)27 + "[9m" + message; } if (message.equalsIgnoreCase("!r")) { message = (char)27 + "[0m" + message; message = (char)27 + "[39m" + message; message = (char)27 + "[49m" + message; } if (message.equalsIgnoreCase("!i")) { message = (char)27 + "[3m" + message; } if (message.equalsIgnoreCase("!u")) { message = (char)27 + "[4m" + message; } if (message.equalsIgnoreCase("!red")) { message = (char)27 + "[31m" + message; } if (message.equalsIgnoreCase("!yellow")) { message = (char)27 + "[33m" + message; } if (message.equalsIgnoreCase("!green")) { message = (char)27 + "[32m" + message; } if (message.equalsIgnoreCase("!blue")) { message = (char)27 + "[34m" + message; } if (message.equalsIgnoreCase("!cyan")) { message = (char)27 + "[36m" + message; } if (message.equalsIgnoreCase("!magenta")) { message = (char)27 + "[35m" + message; } if (message.equalsIgnoreCase("!black")) { message = (char)27 + "[30m" + message; } if (message.equalsIgnoreCase("!redbg")) { message = (char)27 + "[41m" + message; } if (message.equalsIgnoreCase("!yellowbg")) { message = (char)27 + "[43m" + message; } if (message.equalsIgnoreCase("!greenbg")) { message = (char)27 + "[42m" + message; } if (message.equalsIgnoreCase("!bluebg")) { message = (char)27 + "[44m" + message; } if (message.equalsIgnoreCase("!cyanbg")) { message = (char)27 + "[46m" + message; } if (message.equalsIgnoreCase("!magentabg")) { message = (char)27 + "[45m" + message; } if (message.equalsIgnoreCase("!blackbg")) { message = (char)27 + "[40m" + message; } if (message.equalsIgnoreCase("!peopleon")) { System.out.println("People on " + peopleOn); } if(message.equalsIgnoreCase(GroupChat.TERMINATE)) { peopleOn.remove(name); finished = true; socket.leaveGroup(group); socket.close(); break; } message = name + ": " + message; byte[] buffer = message.getBytes(); DatagramPacket datagram = new DatagramPacket(buffer,buffer.length,group,port); socket.send(datagram); } } catch(SocketException se) { System.out.println("Error creating socket"); se.printStackTrace(); } catch(IOException ie) { System.out.println("Error reading/writing from/to socket"); ie.printStackTrace(); } } } public static String Filter(String message, String[] wordsToRemove) { for (String s : wordsToRemove) { if (message.contains(s)) { for (char c : message.toCharArray()) { if (c == ' ') { continue; } message = message.replace(c, '*'); } } } return message; } public static void banFilter(String name, String[] banList) { for (String s : banList) { if (name.equals(s)) { System.out.println("You are banned"); finished = true; banned = true; return; } } } } class ReadThread implements Runnable { private MulticastSocket socket; private InetAddress group; private int port; private static final int MAX_LEN = 1000; ReadThread(MulticastSocket socket,InetAddress group,int port) { this.socket = socket; this.group = group; this.port = port; } @Override public void run() { while(!GroupChat.finished) { byte[] buffer = new byte[ReadThread.MAX_LEN]; DatagramPacket datagram = new DatagramPacket(buffer,buffer.length,group,port); String message; try { socket.receive(datagram); message = new String(buffer,0,datagram.getLength(),"UTF-8"); if(!message.startsWith(GroupChat.name)) System.out.println(message); } catch(IOException e) { System.out.println("You have left the chat"); } } } }

I hope you enjoyed and if you know a better way to do ascii art please tell me
credits:

@Ch1ck3n
for the idea on how to make ascii art like
@ruiwenge2
's socket.io tutorial

Voters
Profile icon
Khiekhiekhie
Profile icon
Bunnytoes