123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Net.WebSockets;
- using System.Text;
- using System.Threading.Tasks;
- namespace WebSocketServerSrez
- {
- public class Program
- {
- static void Main(string[] args)
- {
- WebSocketServer(11000);
- }
- public static void WebSocketServer(int port)
- {
- IPHostEntry iPHostEntry = Dns.GetHostEntry("localhost");
- IPAddress iPAddress = iPHostEntry.AddressList[0];
- IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, port);
- Socket socketListener = new Socket(iPAddress.AddressFamily, SocketType.Stream,ProtocolType.Tcp);
- try
- {
- socketListener.Bind(iPEndPoint);
- socketListener.Listen(10);
- while (true)
- {
- Socket handler = socketListener.Accept();
- string data = null;
- byte[] bytes = new byte[1024];
- int bytesRec = handler.Receive(bytes);
- // Tut
- data += Encoding.UTF8.GetString(bytes, 0, bytesRec);
- var dataR = data.Split(' ');
- if (dataR[0] == "SetUserSession")
- {
- Session.IdUser = Convert.ToInt32(dataR[1]);
- Session.DateStart = DateTime.Now;
- SetSessionUser setSessionUser = new SetSessionUser()
- {
- id_user = Convert.ToInt32(dataR[1]),
- date_start = DateTime.Now.ToString()
- };
- var dataSend = JsonConvert.SerializeObject(setSessionUser, Formatting.Indented);
- var request = WebRequest.Create("http://127.0.0.1:5000/set_session_user");
- request.ContentType = "application/json";
- request.Method = "POST";
- using (var sw = new StreamWriter(request.GetRequestStream()))
- {
- sw.Write(dataSend);
- }
- var response = request.GetResponse();
- using (var sr = new StreamReader(response.GetResponseStream()))
- {
- var jsonResp = JsonConvert.DeserializeObject<RespSetSessionUser>(sr.ReadToEnd());
-
- byte[] mes = Encoding.UTF8.GetBytes(jsonResp.id_user_session.ToString());
- handler.Send(mes);
- handler.Shutdown(SocketShutdown.Both);
- handler.Close();
- }
- }
- else if (dataR[0] == "UpdateUserSession")
- {
- Session.DateFinish = DateTime.Now;
- var request = WebRequest.Create("http://127.0.0.1:5000/update_session_user");
- request.ContentType = "application/json";
- request.Method = "POST";
- UpdateSessionUser updateSessionUser = new UpdateSessionUser()
- {
- Id = Convert.ToInt32(dataR[1]),
- date_finish = Session.DateFinish.ToString()
- };
- var dataSend = JsonConvert.SerializeObject(updateSessionUser, Formatting.Indented);
- using (var sw = new StreamWriter(request.GetRequestStream()))
- {
- sw.Write(dataSend);
- }
- request.GetResponse();
- }
- }
- }
- catch (Exception)
- {
- throw;
- }
- finally
- {
- }
- }
- public class SetSessionUser
- {
- public int id_user { get; set; }
- public string date_start { get; set; }
- }
- public class RespSetSessionUser
- {
- public int id_user_session { get; set; }
- }
- public class UpdateSessionUser
- {
- public int Id { get; set; }
- public string date_finish { get; set; }
- }
- }
- }
|