Migrated from tutorial-sockets v3

This commit is contained in:
Araq 2010-09-14 03:11:06 -07:00
parent bec1d30c93
commit 4cc3d25850
1 changed files with 9 additions and 7 deletions

View File

@ -6,7 +6,8 @@ After that you can use <code>s.connect("addr", TPort(80))</code> to connect to a
Once you connect to a server, you can send and receive messages to/from the server by using <code>s.send("Message")</code> to send, and <code>s.recv()</code> to receive. Once you connect to a server, you can send and receive messages to/from the server by using <code>s.send("Message")</code> to send, and <code>s.recv()</code> to receive.
== Examples == == Examples ==
=== IRC Bot === === IRC Bot ===
<pre><nowiki>import sockets, strutils <pre><nowiki>
import sockets, strutils
when not defined(strutils.toStringSep): when not defined(strutils.toStringSep):
proc toStringSep(x: int64): string = proc toStringSep(x: int64): string =
@ -53,18 +54,19 @@ while True:
s.send("JOIN #nimrod\c\L") s.send("JOIN #nimrod\c\L")
if data.split()[1] == "PRIVMSG": if data.split()[1] == "PRIVMSG":
if data.split()[3] == ":!about": if data.split()[3] == ":!about":
var msg: string = "NimrodBot 0.1 compiled on " & CompileDate & " " & CompileTime var msg = "NimrodBot 0.1 compiled on " & CompileDate & " " & CompileTime
msg = msg & " running on " & hostOS msg.add(" running on " & hostOS)
s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L") s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L")
elif data.split()[3] == ":!mem": elif data.split()[3] == ":!mem":
var msg: string = "Occupied memory: " & formatSize(int64(getOccupiedMem())) var msg = "Occupied memory: " & formatSize(int64(getOccupiedMem()))
s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L") s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L")
elif data.split()[3] == ":!freemem": elif data.split()[3] == ":!freemem":
var msg: string = "Free memory: " & formatSize(int64(getFreeMem())) var msg = "Free memory: " & formatSize(int64(getFreeMem()))
s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L") s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L")
elif data.split()[3] == ":!totalmem": elif data.split()[3] == ":!totalmem":
var msg: string = "Total memory: " & formatSize(int64(getTotalMem())) var msg = "Total memory: " & formatSize(int64(getTotalMem()))
s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L")</nowiki></pre> s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L")
</nowiki></pre>