Created
June 12, 2015 08:37
-
-
Save peace2048/dcbacc37ff4058bf79ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Net | |
open System.Net.Sockets | |
type IpMessenger (port: int) = | |
let client = new UdpClient(port) | |
let random = new Random(Environment.TickCount) | |
let nextId = fun () -> random.Next(0, 1000) | |
let sendBuffer (address: IPAddress, message: Byte[]) = | |
client.Send(message, message.Length, new IPEndPoint(address, 2425)) | |
let makeMessage (command: int, message: string) = | |
String.Format("1:{0}:sbot:VM-OCHIAI8:{1}:{2}", nextId(), command, message) | |
member x.send (address: IPAddress, command: int, ?message: String) = | |
let buffer = Encoding.UTF8.GetBytes(makeMessage(command, defaultArg message String.Empty)) | |
sendBuffer(address, buffer) | |
interface IDisposable with | |
member x.Dispose() = client.Close() | |
using (new IpMessenger(12425)) (fun (ipmsg: IpMessenger) -> | |
(* 自分をブロードキャストで通知 別に要らないです | |
ipmsg.send(IPAddress.Broadcast, 1) |> ignore | |
*) | |
ipmsg.send(IPAddress.Loopback, 32, "Hello") |> ignore) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment