Board logo

标题: [小试牛刀]局域网vbs聊天 [打印本页]

作者: 523066680     时间: 2009-9-14 11:16    标题: [小试牛刀]局域网vbs聊天
这阵子终于可以经常碰电脑拉!!!

刚尝试,脚本写的不完整,比如一些输出输入到后来没有 .close

这个脚本是刚测试成功,所以只带有聊天部分,网络配置得事先按要求配置好。

需要环境:
两台电脑互相开启了目录共享并映射给对方, 映射盘符为X:
共享目录下建立 chat 文件夹,里面放入下面的 “talktest.vbs ”

其中一方执行,脚本提示
等待响应..

待对方执行后
.双方连接完成!

(我是通过删除或建立某个指定文件来判断的^_^,没啥好方法了,因为
情况可能有很多种。。。先打开a: 如果存在x 则等待x消失 ,
b后打开: 如果存在x 则删除x
这样子来响应的。不存在则反)

之后输入字符,对方应该是可看到的,我这边的另一台电脑是可以。

要结束的时候输入 end 并回车~ 对方也会得到结束的提示。


set wsx=createobject("wscript.shell")
if wscript.arguments.count = 0 then
wsx.run _
"cmd /c mode con cols=80 lines=25 &cscript //logo " _
&wscript.scriptname & " 0",3
wsx.run _
"cmd /c mode con cols=80 lines=5 &title Inputbox &cscript //nologo " _
&wscript.scriptname & " 1",3
wscript.quit
elseif wscript.arguments(0) = 1 then
call readinput()
wscript.quit
elseif wscript.arguments(0) = 0 then
call scroll()
wscript.quit
end if

sub readinput()
bok="x:\chat\talk.x"
btalk="x:\chat\talk.txt"
aok="talk.x"
atalk="talk.txt"
set fs=createobject("scripting.filesystemobject")
if fs.fileexists(aok) then
fs.deletefile(aok)
else
fs.createtextfile(aok)
end if

set writex=fs.createtextfile(atalk,true)
do
wscript.stdout.write "say:"
set inp=wscript.stdin
inpinfo=inp.readline
writex.writeline inpinfo
loop until inpinfo = "end"
wscript.sleep 1000
wscript.quit
end sub

sub scroll()
bok="x:\chat\talk.x"
btalk="x:\chat\talk.txt"
aok="talk.x"
atalk="talk.txt"
set wsx=createobject("wscript.shell")
set fs=createobject("scripting.filesystemobject")

set output=wscript.stdout
for i = 1 to 3
output.writeline ""
next
output.write "等待响应"

if fs.fileexists(bok) then
call wait(bok,true) '因存在而等待
else
call wait(bok,false) '因不存在而等待
end if

if fs.fileexists(aok) then
fs.deletefile(aok)
else
fs.createtextfile(aok)
end if
output.writeline "双方连接完成! " &vbcrlf & "对方: " &vbcrlf

call wait(btalk,false) '因不存在而等待

set talkx=fs.opentextfile(btalk,1)
set oursx=fs.opentextfile(atalk,1)
do
if talkx.atendofstream then
wscript.sleep 800
else
info=talkx.readline
output.writeline info
end if
if oursx.atendofstream = false then
if oursx.readline = "end" then
output.writeline "Closing..."
wscript.sleep 1000
wscript.quit
end if
end if
loop until info = "end"
output.writeline "Script: He was close the chatbox. "
output.writeline "Script: Input 'end' (at smallbox) and press enter to exit..."
while oursx.atendofstream = true
wscript.sleep 800
wend
wscript.quit
end sub

sub wait(filename,have)
set fs=createobject("scripting.filesystemobject")
set output=wscript.stdout
while fs.fileexists(filename) = have
output.write "."
wscript.sleep 1000
wend
End sub


Last edited by 523066680 on 2009-9-14 at 12:20 ]

作者: 523066680     时间: 2009-9-14 11:18
如果要强制对方跳出聊天框,也可以,那边后台执行一个脚本随时恭候……

不过那样就有点想要做坏事了……

Last edited by 523066680 on 2009-9-14 at 22:13 ]

作者: 523066680     时间: 2009-9-14 22:19
说明: 首先让自己以命令行界面执行,我喜欢的就是cmd的字符界面。
本来想用批处理做,配置网络的话句子也方便,但是考虑批处理字符处理
和效率部分,还是决定用cscript _.vbs 来实现我想要的字符界面。
刚做的时候觉得不会有什么难的,不就双方弄个临时文件互相沟通么?
做的时候就郁闷了,分谁先打开,谁后打开,怎么判断后打开的已经响应。
而且两个脚本的内容同一,一致。想了挺久还是用特定文件的存在判断,
机制如1楼所说。
其他部分就是做了很多体力了……

Last edited by 523066680 on 2009-9-14 at 22:20 ]

作者: image72     时间: 2009-10-25 22:25
牛啊,为什么不用HTA做交互界面呢?
这样不更容易控制~

作者: 523066680     时间: 2009-10-26 07:59
因为不会弄,真是一点不会。