Package chat

Source Code of chat.Chat

package chat;

import io.socket.SocketIO;

import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

public class Chat extends Thread {
    private SocketIO socket;
    private ChatCallback callback;
   
    public Chat(ChatCallbackAdapter callback) {
        this.callback = new ChatCallback(callback);
    }
   
    @Override
    public void run() {
        try {
      socket = new SocketIO("http://localhost:3000", callback);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
    }
   
    public void sendMessage(String message) {
        try {
            JSONObject json = new JSONObject();
            json.putOpt("message", message);
            socket.emit("user message", json);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }
   
    public void join(String nickname) {
        try {
            JSONObject json = new JSONObject();
            json.putOpt("nickname", nickname);
            socket.emit("nickname", callback, json);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
TOP

Related Classes of chat.Chat

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.