VioletaBabel
37. 채팅 서버 만들기 본문
웹소켓 버전 차이로
를 클라이언트 프로젝트에 넣고
기존 참조의 웹소켓샾은 지우고, 참조 우클릭-참조 추가-찾아보기로 이 파일을 넣어줌.
=====서버 프로젝트 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!--MainWIndow.xaml--> <Window x:Class="_180604_websocketTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:_180604_websocketTest" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Button Name ="Start" Content="Start" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="713,325,0,0" Click="Start_Click"/> <Button Name ="Stop" Content="Stop" HorizontalAlignment="Left" Margin="713,362,0,0" VerticalAlignment="Top" Width="75" Click="Stop_Click"/> <TextBox Name ="Console" AcceptsReturn="True" HorizontalAlignment="Left" Height="343" TextWrapping="Wrap" Text="" IsReadOnly ="True" VerticalAlignment="Top" Width="653" Margin="34,39,0,0"/> <!-- TextBox의 AcceptsReturn이 트루이면 엔터키를 먹는 텍스트박스가 됨. IsReadOnly가 트루면 읽기 전용--> </Grid> </Window> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | //MainWindow.xaml.cs using System; using System.Windows; using WebSocketSharp.Server; namespace _180604_websocketTest { public partial class MainWindow : Window { private static MainWindow g_Main = null; public static MainWindow getWindow { get { return g_Main; } } public void addText(string text) { this.Dispatcher.Invoke ( (Action)(() => { string myText = Console.Text; myText += text; Console.Text = myText; }) ); } public MainWindow() { InitializeComponent(); g_Main = this; } private HttpServer webSocketServer = null; private void Start_Click(object sender, RoutedEventArgs e) { webSocketServer = new HttpServer(14649); webSocketServer.OnGet += (sen, ex) => { var req = ex.Request; var res = ex.Response; }; webSocketServer.AddWebSocketService<Echo>("/Echo"); webSocketServer.AddWebSocketService<Chat>("/Chat"); webSocketServer.Start(); Console.Text += "::서버시작\n"; } /*public Chat chatttttt() { return new Chat("A"); }*/ // 람다 식 안쓰려면 이렇게 2 private void Stop_Click(object sender, RoutedEventArgs e) { if (webSocketServer != null) webSocketServer.Stop(); webSocketServer = null; Console.Text += "서버종료\n"; } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //Echo.cs using System; using System.Threading.Tasks; using System.Windows; using WebSocketSharp; using WebSocketSharp.Server; namespace _180604_websocketTest { public class Echo : WebSocketBehavior { protected override void OnMessage(MessageEventArgs e) { var text = e.Data; text += "\n"; MainWindow win = MainWindow.getWindow; win.addText(text); Send("보내자"); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | //Chat.cs using System; using System.Threading.Tasks; using WebSocketSharp; using WebSocketSharp.Server; namespace _180604_websocketTest { public class Chat : WebSocketBehavior { private string _suffix; public Chat() : this(null) // 생성자를 그냥 만들 시 밑의 오버로드된 생성자의 인수 suffix에 null을 넣어 실행한다. { } public Chat(string suffix) { _suffix = suffix ?? String.Empty; //?? 연산자는 연산자 앞의 값이 null이면 뒤의 값을 대입. } protected override void OnMessage(MessageEventArgs e) { var text = e.Data; text += "\n"; MainWindow win = MainWindow.getWindow; win.addText(text); Sessions.Broadcast(e.Data); } } } | cs |
=======클라이언트
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | //Cl.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using WebSocketSharp; namespace _180604_websocketClientTest { class Cl { WebSocket mWebSocket; public Cl() { StartConnect(); } public void StartConnect() { //mWebSocket = new WebSocket("ws://localhost:14649/Chat"); // 이건 내가 테스트할 때. mWebSocket = new WebSocket("ws://192.168.0.21:14649/Chat"); mWebSocket.OnOpen += (sender, e) => { }; mWebSocket.OnMessage += (sender, e) => { MainWindow.mainWindow.recvMsg(e.Data); }; mWebSocket.Connect(); } public void SendMessage(string msg) { mWebSocket.Send("들숨에재물을날숨에건강을 : "+msg); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | //MainWindow.xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace _180604_websocketClientTest { /// <summary> /// MainWindow.xaml에 대한 상호 작용 논리 /// </summary> public partial class MainWindow : Window { Cl c; private static MainWindow _mainWindow = null; public static MainWindow mainWindow { get { return _mainWindow; } } public MainWindow() { InitializeComponent(); _mainWindow = this; c = new Cl(); } public void recvMsg(string text) { this.Dispatcher.Invoke(() => { RecvBox.Text += text+"\n"; } ); } private void sendButton_Click(object sender, RoutedEventArgs e) { c.SendMessage(sendBox.Text); sendBox.Text = ""; } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!--MainWindow.xaml--> <Window x:Class="_180604_websocketClientTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:_180604_websocketClientTest" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <TextBox x:Name="RecvBox" HorizontalAlignment="Left" Height="374" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="614" Margin="10,0,0,0"/> <Button x:Name="sendButton" Content="Send" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="624,379,0,0" Click="sendButton_Click" Height="30"/> <TextBox x:Name="sendBox" HorizontalAlignment="Left" Height="30" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="614" Margin="10,379,0,0"/> </Grid> </Window> | cs |
'BCA > 5. Websocket-Sharp' 카테고리의 다른 글
36. 간단한 서버 연결 (0) | 2018.06.04 |
---|
Comments