본문 바로가기
TechNical/WMI

[HTA] 네트워크드라이브를 이용한 채팅

by 강멍멍이 강멍멍이 2020. 2. 23.
반응형

어쩌다 보니.. HTA로 별걸 다 하게 되었다.

웹서버가 있으면 좋겠지만, 없는 상황에서 비스끄무리 하게 할 수 없을까란

생각에서 출발해서 만든거다.

아이디어는 간단하다.

어차피 서버에서 일부 정보를 읽고 쓰고 하기만 하면 되니까

네트워크 드라이브를 이용해서 뭔가 수작을 부려 보았다.

사실 네트워크 프로그래밍이 아니다 보니까 훨씬 더 간단하게 만들 수 있다.

좌측에 채팅 내용이 표기되고 우측에 파일 업로드/다운로드 목록을 볼 수 있다.

 

[NetworkDriveChat.hta]

<html>
    <meta charset="euc-kr">
    <head>
        <title>NetworkDrive Chat</title>
        <HTA:APPLICATION 
         ID="강멍멍이"
         APPLICATIONNAME="NetworkDrive Chat"
         SCROLL="No"
         SINGLEINSTANCE="yes"
         WINDOWSTATE="normal"
        />
        
        <style>
            BODY {
                font-size:9pt;
                font-family:돋움체;
            }
            
            TABLE, TR, TD {
                font-size:8pt;
                border:1px solid black;
                border-collapse:collapse;
                padding-left=3px;
                padding-right=3px;
            }            
            
            .hide {
                opacity : 0;
                transition: opacity 1s linear;
                -webkit-transition: opacity 1s linear;
            }
        </style>
     
        <script language="VBScript">
            Dim timerID, interval, timerOff, timerOffTime, hearbeatInterval
            Dim homeDir, chatListName, fileListName
            
            Sub Window_OnLoad
                window.resizeTo 950, 600
                window.moveTo 750, 250
                                
                homeDir = "\\127.0.0.1\멍멍\CHAT\"
                chatListName = homeDir & "CHAT.dat"
                fileListName = homeDir & "FILE.dat"
                
                interval = 3000 '3초 단위
                timerOff = 0
                timerOffTime = 15 * 3 '3분 휴면시 자동갱신 중지
                hearbeatInterval = 15 * 30 '30분 뒤에 새로운거 있나 체크
                                
                Call readFile
            End Sub
            
            Sub readFile
                timerOff = timerOff + 1
                
                IF timerOff > timerOffTime THEN   '휴면
                    chatViewDiv.style.backgroundColor = "eeeeee"
                    fileViewDiv.style.backgroundColor = "eeeeee"
                    'window.clearTimeOut(timerID)

                    IF timerOff > hearbeatInterval THEN '가끔씩 혹시나 갱신된게 있나 체크
                        timerOff = 0
                    END IF
                ELSE
                    chatViewDiv.style.backgroundColor = "fff6ec"
                    fileViewDiv.style.backgroundColor = "ffeeea"                
                
                    On Error Resume Next
                    Set objFSO = CreateObject("Scripting.FileSystemObject")
                    
                    '채팅 목록
                    If objFSO.FileExists(chatListName) Then    
                        Set objFileR = objFSO.OpenTextFile(chatListName, 1)
                        chatFull = ""
                        
                        Do Until objFileR.AtEndOfStream
                            curLine = objFileR.ReadLine
    
                            IF chatFull = "" THEN
                                chatFull = curLine
                            ELSE
                                chatFull = chatFull & "<br>" & curLine
                            END IF
                        Loop
                        
                        chatViewDiv.innerHTML = chatFull
                        objFileR.close
                    End IF
                    
                    Set element = document.getElementById("chatViewDiv")
                    element.scrollTop = element.scrollHeight
    
                    '파일 목록
                    If objFSO.FileExists(fileListName) Then    
                        Set objFileR = objFSO.OpenTextFile(fileListName, 1)
                        fileList = ""
                        
                        Do Until objFileR.AtEndOfStream
                            curLine = objFileR.ReadLine
                            convLine = "<a href=""file:///" & homeDir & "FILE\" & curLine & """>" & curLine & "</a>"
                            convLine = convLine & "&nbsp;&nbsp;<button style=""font-size:8pt;width=14%;height=14pt"" onClick=""deleteFile('" & curLine & "')"">삭제</button>" 
                            IF fileList = "" THEN
                                fileList = convLine
                            ELSE
                                fileList = fileList & "<br><br>" & convLine
                            END IF
                        Loop
                        
                        fileViewDiv.innerHTML = fileList
                        objFileR.close
                    End IF
                    
                    Set element = document.getElementById("fileViewDiv")
                    element.scrollTop = element.scrollHeight
                END IF
                
                window.clearTimeOut(timerID)
                timerID = window.setTimeOut("readFile", interval)
            End Sub
            
            Sub sendChat
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set objFileW = objFSO.OpenTextFile(chatListName, 8) 'APPEND
                objFileW.WriteLine inpName.value & " : " & inpChat.value
                objFileW.close
                inpChat.value = ""

                IF timerOff >= timerOffTime THEN
                    timerOff = 0
                    Call readFile
                END IF
                                    
                timerOff = 0
            End Sub
            
            Sub enterChat
                IF window.event.Keycode = 13 THEN
                    sendChat
                END IF
            End Sub

            Sub chkSelectChange
                timerOff = 0
                
                IF ( fileForm.seletedFile.value <> "" ) THEN
                
                    Set objFSO = CreateObject("Scripting.FileSystemObject")                    

                    fullPathArr = Split(fileForm.seletedFile.value, "\")
                    fileNameIdx = UBound(fullPathArr)
                    
                    
                    If objFSO.FileExists(homeDir & "\FILE\" & fullPathArr(fileNameIdx)) Then  
                        MsgBox "동일한 파일이 존재 합니다!", 0, "멍멍"
                        window.fileForm.reset()
                        Exit Sub
                    End If                
                                    
                    objFSO.CopyFile fileForm.seletedFile.value, homeDir & "\FILE\"                    

                    Set objFileW = objFSO.OpenTextFile(fileListName, 8) 'APPEND
                    objFileW.WriteLine fullPathArr(fileNameIdx)
                    objFileW.close
                    
                    MsgBox "업로드 되었습니다!", 0, "멍멍"
                    window.fileForm.reset()
                    
                END IF
            End Sub
            
            Sub deleteFile(getFileName)
                        
                result = MsgBox (getFileName & " 파일을 삭제하시겠습니까?", vbYesNo, "멍멍")
                
                IF result <> vbYes THEN
                    MsgBox "삭제를 취소 합니다.", 0, "멍멍"
                    Exit Sub
                END IF
                
                '파일 삭제
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                objFSO.deleteFile homeDir & "\FILE\" & getFileName
                
                '목록 갱신
                If objFSO.FileExists(fileListName) Then
                    Set objFileR = objFSO.OpenTextFile(fileListName, 1)
                    fileList = ""
                    
                    Do Until objFileR.AtEndOfStream
                        curLine = objFileR.ReadLine
                        IF fileList = "" THEN
                            fileList = curLine
                        ELSE
                            fileList = fileList & vbCrLf & curLine
                        END IF
                    Loop

                    objFileR.close                    

                    Set objFileW = objFSO.OpenTextFile(fileListName, 2) 'WRITE
                    
                    arrFileList = Split(fileList, vbCrLf)
                    For Each fileName in arrFileList
                        '삭제한거는 빼고 목록 재작성
                        IF fileName <> getFileName THEN
                            objFileW.WriteLine fileName
                        END IF
                    Next
                    
                    objFileW.close
                    
                    MsgBox "삭제 되었습니다!", 0, "멍멍"
                End IF          
                
            End Sub
        </script>
    </head>
    
    <body>
        <div id="chatViewDiv" style="width:70%;height:93%;float:left;margin-top:8px;margin-right:8px;padding-top:5px;padding-left:5px;overflow:auto;line-height:1.4;background-color:fff6ec"></div>        
        <div id="fileViewDiv" style="width:29%;height:83%;float:left;margin-top:8px;padding-top:3px;padding-left:5px;overflow:auto;background-color:ffeeea"></div>
        <form id="fileForm"><input id="seletedFile" type="file" onChange="chkSelectChange" style="width=100%"></form>
        <div id="chatInpDiv" onKeyDown="enterChat" style="width:100%;height:12%;float:left;padding-top:1px;overflow:auto;">
            <input id="inpName" value="멍멍" style="width:12%">
            <input id="inpChat" style="width:87%">&nbsp;&nbsp;
        </div>
    </body>
</html>

 

반응형

댓글0