Skip to content

Instantly share code, notes, and snippets.

@dom96
Created June 4, 2015 18:15
Show Gist options
  • Select an option

  • Save dom96/eba8781220ee2c015c1d to your computer and use it in GitHub Desktop.

Select an option

Save dom96/eba8781220ee2c015c1d to your computer and use it in GitHub Desktop.
import os, asyncnet, asyncdispatch
type
CBOnClientMessage* = proc(message: string)
proc handleClient(sockClient: AsyncSocket, cb: CBOnClientMessage) {.async.} =
while true:
let line = await sockClient.recvLine()
if "" == line:
break
await sockClient.send("TOKEN")
cb(line)
echo(line)
sockClient.close()
proc startServerTask*(sockServer: AsyncSocket, port: int, cb: CBOnClientMessage) {.async.} =
echo("Server starting")
sockServer.bindAddr(port.Port)
sockServer.listen()
while true:
echo("Waiting for client")
let sockClient = await sockServer.accept()
asyncCheck handleClient(sockClient, cb)
sockServer.close()
proc stopServer*(sockServer: AsyncSocket) =
sockServer.close()
import asyncdispatch
import asyncnet
import iup
import server
const SERVER_PORT = 1987
var sockServer = newAsyncSocket()
proc updateChatBox(message: string) =
var textChatBox = getHandle("textChatBox")
setAttribute(textChatBox, "VALUE", message)
# echo(message)
proc updateStatusBarMessage(message: string) =
var labelStatusBar = getHandle("labelStatusBar")
setAttribute(labelStatusBar, "TITLE", message)
proc onButtonStart(handle: PIhandle) :cint {.cdecl.} =
var buttonStart = getHandle("buttonStart")
var buttonStop = getHandle("buttonStop")
setAttribute(buttonStart, "ACTIVE", "NO")
setAttribute(buttonStop, "ACTIVE", "YES")
asyncCheck startServerTask(sockServer, SERVER_PORT, updateChatBox)
return IUP_DEFAULT
proc onButtonStop(handle: PIhandle) :cint {.cdecl.} =
var buttonStart = getHandle("buttonStart")
var buttonStop = getHandle("buttonStop")
setAttribute(buttonStart, "ACTIVE", "YES")
setAttribute(buttonStop, "ACTIVE", "NO")
stopServer(sockServer)
return IUP_DEFAULT
proc startGUI() =
discard iup.open(nil, nil)
var buttonStart = button("start", "onButtonStart")
var buttonStop = button("stop", "onButtonStop")
discard setCallback(buttonStart, "ACTION", onButtonStart)
discard setCallback(buttonStop, "ACTION", onButtonStop)
discard setHandle("buttonStart", buttonStart)
discard setHandle("buttonStop", buttonStop)
setAttribute(buttonStart, "ACTIVE", "YES")
setAttribute(buttonStop, "ACTIVE", "NO")
var textChatBox = text("")
var labelStatusBar = label("nothing...")
setAttribute(textChatBox, "MULTILINE", "YES")
setAttribute(textChatBox, "EXPAND", "YES")
setAttribute(textChatBox, "APPENDNEWLINE", "YES")
setAttribute(textChatBox, "SCROLLBAR", "YES")
setAttribute(textChatBox, "AUTOHIDE", "YES")
setAttribute(textChatBox, "READONLY", "YES")
setAttribute(textChatBox, "BGCOLOR", "0 0 0")
setAttribute(textChatBox, "FGCOLOR", "255 255 255")
setAttribute(labelStatusBar, "EXPAND", "HORIZONTAL")
discard setHandle("textChatBox", textChatBox)
discard setHandle("labelStatusBar", labelStatusBar)
var hboxTop = hbox(nil)
discard insert(hboxTop, nil, buttonStop)
discard insert(hboxTop, nil, buttonStart)
var vboxMainView = vbox(nil)
discard insert(vboxMainView, nil, labelStatusBar)
discard insert(vboxMainView, nil, textChatBox)
discard insert(vboxMainView, nil, hboxTop)
var dialogMainWindow = dialog(vboxMainView)
setAttribute(dialogMainWindow, "TITLE", "server")
setAttribute(dialogMainWindow, "BORDER", "YES")
setAttribute(dialogMainWindow, "RESIZE", "NO")
setAttribute(dialogMainWindow, "SIZE", "QUARTERxQUARTER")
discard showXY(dialogMainWindow, IUP_CENTER, IUP_CENTER)
while true:
discard loopStep()
try:
asyncdispatch.poll(1)
except ValueError:
discard
close()
startGUI()
@sepisoad
Copy link
Copy Markdown

sepisoad commented Jun 4, 2015

THANK YOU MAN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment