Advertisement
Yurry

Untitled

Apr 27th, 2011
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.25 KB | None | 0 0
  1. open System
  2. open System.ServiceModel
  3. open System.ServiceModel.Security
  4. open System.Security.Cryptography.X509Certificates
  5. open System.Windows.Forms
  6.  
  7. (* Create a window and set a few properties *)
  8. let form = new Form(Visible=true, TopMost=true, Text="Welcome to F#")
  9.  
  10. let login = new TextBox(Location = new Drawing.Point(5, 5), Size = new Drawing.Size(64, 16))
  11. let pass = new TextBox(Location = new Drawing.Point(80, 5), Size = new Drawing.Size(64, 16))
  12. let btn = new Button(Location = new Drawing.Point(180, 5), Text="Connect")
  13. let log = new TextBox(Location = new Drawing.Point(5, 32), Size = new Drawing.Size(256, 64), ReadOnly = true)
  14. log.Multiline <- true
  15. let msg = new TextBox(Location = new Drawing.Point(5, 128), Size = new Drawing.Size(256, 64), ReadOnly = true)
  16.  
  17. type ChatCallback = class
  18.     new () = {}
  19.     interface IChatCallback with
  20.         member p.Message(who: string, text: string) =
  21.             ()
  22.             //log.AppendText(sprintf "%s: %s\n" who text)
  23. end
  24.  
  25. (* Add the label to the form *)
  26. form.Controls.Add(login)
  27. form.Controls.Add(pass)
  28. form.Controls.Add(btn)
  29. form.Controls.Add(msg)
  30. form.Controls.Add(log)
  31.  
  32. (* Finally, run the form  *)
  33. let binding = new NetTcpBinding()
  34. binding.Security.Mode <- SecurityMode.Message
  35. binding.Security.Message.ClientCredentialType <- MessageCredentialType.UserName
  36. let client = new ChatClient(new InstanceContext(new ChatCallback()),
  37.                             binding,
  38.                             new EndpointAddress(new Uri("net.tcp://127.0.0.1/Pochtochat/Chat"),
  39.                                                 EndpointIdentity.CreateDnsIdentity("localhost")))
  40. client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode <- X509CertificateValidationMode.None
  41.  
  42. btn.Click.Add(fun _ ->
  43.     client.ClientCredentials.UserName.UserName <- login.Text
  44.     client.ClientCredentials.UserName.Password <- pass.Text
  45.     client.Open()
  46.     client.Login()
  47.     msg.ReadOnly <- false)
  48.    
  49. msg.KeyDown.AddHandler(fun _ (e:KeyEventArgs) ->
  50.     if e.KeyCode = Keys.Return then
  51.         client.SaySomething(msg.Text)
  52.         msg.Clear()
  53.     ())
  54.  
  55. do Application.Run(form)
  56.  
  57. if client.State = CommunicationState.Opened then                
  58.     client.Logout()
  59.     client.Close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement