gnetchat.bmx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. Strict
  2. Import BRL.GNet
  3. AppTitle="GNet Test2"
  4. Local host:TGNetHost=CreateGNetHost()
  5. Local me:TGNetObject
  6. Local chat$,info$
  7. Graphics 800,600,0,15
  8. Repeat
  9. Local c=GetChar()
  10. Select c
  11. Case 8
  12. If chat chat=chat[..chat.length-1]
  13. Case 27
  14. If Confirm( "Quit?" )
  15. CloseGNetHost host
  16. End
  17. EndIf
  18. Case 13
  19. If chat.find("/")=0
  20. chat=chat[1..]
  21. Local cmd$=chat
  22. Local arg$
  23. Local i=chat.find(" ")
  24. If i<>-1
  25. cmd=chat[..i]
  26. arg=chat[i+1..]
  27. EndIf
  28. Select cmd
  29. Case "create"
  30. If me
  31. info="Already created"
  32. Else
  33. me=CreateGNetObject( host )
  34. SetGNetString me,0,arg
  35. SetGNetString me,1,"Ready"
  36. EndIf
  37. Case "close"
  38. If me
  39. CloseGNetObject me
  40. me=Null
  41. Else
  42. info="Not created"
  43. EndIf
  44. Case "quit","exit"
  45. CloseGNetHost host
  46. End
  47. Case "nick"
  48. If arg
  49. If me SetGNetString me,0,arg
  50. info="Nick changed to "+arg
  51. Else
  52. info="Expecting arg"
  53. EndIf
  54. Case "listen"
  55. Local port=12345
  56. If arg port=Int(arg)
  57. If GNetListen( host,port )
  58. info="Listening on port "+port
  59. Else
  60. info="Listen failed"
  61. EndIf
  62. Case "connect"
  63. If arg
  64. Local addr$=arg
  65. Local port=12345
  66. Local i=arg.find(":")
  67. If i<>-1
  68. addr=arg[..i]
  69. port=Int(arg[i+1..])
  70. EndIf
  71. If GNetConnect( host,addr,port )
  72. info="Connected to "+addr+":"+port
  73. Else
  74. info="Failed to connect to "+addr+":"+port
  75. EndIf
  76. Else
  77. info="Expecting arg"
  78. EndIf
  79. Default
  80. info="Unrecognized command '"+cmd+"'"
  81. End Select
  82. Else
  83. If me SetGNetString me,1,chat
  84. EndIf
  85. chat=""
  86. Default
  87. If c>31 And c<127 chat:+Chr(c)
  88. End Select
  89. GNetSync host
  90. Cls
  91. Local y,h=GraphicsHeight()
  92. For Local obj:TGNetObject=EachIn GNetObjects( host,GNET_ALL )
  93. If obj.state()=GNET_CLOSED Continue
  94. If obj=me
  95. SetColor 255,255,255
  96. Else
  97. SetColor 0,128,255
  98. EndIf
  99. DrawText GetGNetString( obj,0 )+":"+GetGNetString( obj,1 ),0,y
  100. y:+16
  101. Next
  102. SetColor 255,255,0
  103. DrawText info,0,h-32
  104. SetColor 0,255,0
  105. DrawText ">"+chat,0,h-16
  106. DrawRect TextWidth(">"+chat),h-16,8,16
  107. DrawText "/create nick /listen /connect host /quit /nick newnick",0,h-48
  108. Flip
  109. Forever