我想创建一个更新函数,该函数在提示用户输入" ChatBox"之后立即生效。这样我学校网络上的其他用户就可以输入,所有其他用户都可以看到它而无需重新启动程序或键入空格来重新加载.txt文件
这是我到目前为止编写的代码
@echo off
:start
cls
echo Welcome to ChatBox V1.0
echo Please Enter the Passcode to Enter
set /p pass=
if not "%pass%"=="2730" goto start
:enter
cls
type cblog.txt
echo.
set /p text=
echo %text% >> cblog.txt
goto enter
答案 0 :(得分:0)
我喜欢这个主题,所以我写了一个完整的原型:
@echo off
setlocal
if "%~1" equ "" echo You must give your username as parameter & goto :EOF
set "user=%~1"
set "now=%time%"
echo %now%: User %user% entered the chat room>> msgQueue.txt
call :chatRoom 3< msgQueue.txt
goto :EOF
:chatRoom
rem Omit previous messages
:omitMsg
set /P "msg=" <&3
if "%msg:~0,12%" neq "%now%:" goto omitMsg
echo %msg%
echo/
echo Press S to send a message or eXit to end
echo/
:msgLoop
rem Check for new messages received
:showMsg
set "msg="
set /P "msg=" <&3
if not defined msg goto send
echo %msg%
goto :showMsg
rem Check to send a new message
:send
ver > NUL
choice /C SNX /N /T 3 /D N > NUL
if errorlevel 3 goto end
if errorlevel 2 goto msgLoop
rem Send a message
echo -----------------------------------
set /P "msg=Message: "
echo -----------------------------------
echo %user%: %msg% >> msgQueue.txt
goto msgLoop
:end
echo %time%: User %user% leaved the chat room>> msgQueue.txt
可以在/T 3
命令的choice
参数中调整响应时间:较短的时间会使聊天响应更快,但会消耗更多的CPU时间。
下面的图片显示了聊天室中有四个用户的测试: