mentorchatbot / mentor-chat-bot

Rule based chatbot for job searching
0 stars 0 forks source link

ChatScript socket 통신에 대한 이해 #11

Open JunHoPark93 opened 5 years ago

JunHoPark93 commented 5 years ago

받은 내용에 이런게 있던데

char nullChar = (char) 0;
String botMessage = Client.doMessage(userName + nullChar + nullChar + message + nullChar);

ChatScript 엔진쪽으로 메세지를 보내는 부분인데 null 캐릭터? 를 때려넣는데 이유 분석해봐야 할듯..

(공식 doc 출처: http://chatscript.sourceforge.net/Documentation/ChatScript%20Server%20Manual.pdf)

ChatScript Protocol The message a client sends is a concatenation of three null-terminated text strings. The first string is the user login name. The second is the name of the chatbot to talk to. If this is a null string, the system will connect to the default bot. The third string is the message. If the message is null, this is a start of new conversation. This MUST be the first thing you do with a new user. Ideally you do it whenever a new conversation is starting with that user which is how the system knows the old conversation ended. Usually script will detect that this is the start of a new conversation and say something like “hello” or “welcome back” to indicate the two parties are starting up a new conversation, though through the history file the system may have a lot of information about what has gone on in prior conversations. As long as the user is connected to the webpage, for example, you wouldn’t send a startup message again. Due to the requirement of a unique user name, you NORMALLY require the user to enter a login name once on the client, after which you pass that on each transmission to the server. You can bypass asking for a user name if you always just use the “guest” or “.” user names. The message sent to the server during a conversation should never be null (since that looks like a conversation start). Either always prepend a blank on every line from the user, or add a blank if the user presses ENTER without anything else or pass along the newline/cr character. The chatbot can wait forever for each input (the connection is terminated for each volley) and the only way to know that the human “left” is when the human “comes back” with a start of a new conversation.

JunHoPark93 commented 5 years ago

Socket 연결 수립 부분 원본인데, 클래스 따로 빼서 만들거고 인스턴스 변수로 올릴지 말지는 아직 모르겠음.

public static String doMessage(String mess) 
    {
        Socket echoSocket;
        String resp = "";

        try {
            echoSocket = new Socket("localhost", 1024);
            PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    echoSocket.getInputStream()));
            out.println(mess);
            resp = in.readLine();
            echoSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        return resp;
    }