public WhiteBoardPage()
        {
            InitializeComponent();
            DataContext = this;

            canvasInteraction = new CanvasInteraction(Canvas);

            // Set color picker
            ColorPicker.colorChanged += (object sender, EventArgs args) =>
            {
                DrawingColor = ColorPicker.SelectedColor;
            };
            DrawingColor = ColorPicker.SelectedColor;
            

            // Set thickness picker
            int[] colorTab = {1,2,5,10,15,20,25};
            List<ThicknessClass> thicknessList = new List<ThicknessClass>();
            for (int i = 0; i < colorTab.Length; i++)
            {
                thicknessList.Add(new ThicknessClass(colorTab[i]));
            }
            ThicknessPicker.ItemsSource = thicknessList;
            DrawingThickness = 5;

            isDrawing = false;

            previousAuthor = null;
            users = new Dictionary<String, User>();
        }
示例#2
0
        public LinkIO connect(Action<LinkIO> listener)
        {
            IO.Options opts = new IO.Options();
            Dictionary<String, String> query = new Dictionary<String, String>();
            if(mail != "")
				query.Add("mail", mail);

            if (password != "")
                query.Add("password", password);
			
			query.Add("api_key", api_key);
			
            opts.Query = query;
            opts.AutoConnect = false;
            opts.Transports = ImmutableList.Create<string>(WebSocket.NAME, Polling.NAME);

            socket = IO.Socket("http://" + serverIP, opts);

            socket.On("users", (e) =>
            {
                List<User> users = ((JArray)e).ToObject<List<User>>();
                if (users.Count > usersInRoom.Count)
                {
                    foreach(User user1 in users)
                    {
                        bool found = false;
                        foreach(User user2 in usersInRoom)
                        {
                            if (user1.ID == user2.ID)
                                found = true;
                        }
                        if (!found)
                            userJoinListener.Invoke(user1);
                    }
                }
                else
                {
                    foreach (User user1 in usersInRoom)
                    {
                        bool found = false;
                        foreach (User user2 in users)
                        {
                            if (user1.ID == user2.ID)
                                found = true;
                        }

                        if (!found)
                            userLeftListener.Invoke(user1);
                    }
                }

                usersInRoom = users;
            });
			
			socket.On("error", (Object o) =>
            {
                if (errorHandler != null)
                {
                    string message = (string)((JValue)o);
                    message = message.Replace("\"", "");

                    switch (((string)((JValue)o)).Replace("\"", ""))
                    {
                        case "ACCOUNT ERROR":
                            errorHandler.Invoke(new AccountNotFoundException("Email does not match any account in API."));
                            break;
                        case "PASSWORD ERROR":
                            errorHandler.Invoke(new WrongPasswordException("Password does not match the given Email."));
                            break;
                        case "API_KEY ERROR":
                        default:
                            errorHandler.Invoke(new WrongAPIKeyException("The application does not match with an API key."));
                            break;
                    }
                }
            });

            socket.On("info", (Object o) =>
            {
                JObject evt = (JObject)o;

                currentUser = new User()
                {
                    Name = (String)evt.SelectToken("name"),
                    FirstName = (String)evt.SelectToken("firstname"),
                    Mail = (String)evt.SelectToken("mail"),
                    ID = (String)evt.SelectToken("id"),
                    Role = (String)evt.SelectToken("role")
                };

                Task.Run(() =>
                {
                    connected = true;
                    listener.Invoke(this);
                });
            });

            socket.On(Socket.EVENT_DISCONNECT, () =>
            {
                connected = false;
            });

            socket.On("event", (Object o) =>
            {
                JObject evt = (JObject)o;
                String eventName = (String)evt.SelectToken("type");
                if (eventListeners.ContainsKey(eventName))
                {
                    Task.Run(() =>
                    {
                        eventListeners[eventName].Invoke(new Event(evt, cSharpBinarySerializer));
                    });
                }


            });
            
            socket.Connect();

            return this;
        }
        private void PageLoaded(object sender, RoutedEventArgs r)
        {
            // Config the connect.io instance
            currentUser = lio.getCurrentUser();
            /*async (o) =>
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    
                });
            });*/

            foreach (User user in lio.getAllUsersInCurrentRoom())
            {
                users.Add(user.Mail, user);
            }



            lio.on("clear", async (o) =>
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    canvasInteraction.Clear();
                });

            });

            lio.on("image", async (o) =>
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    Event e = (Event)o;
                    Dictionary<string, dynamic> data = e.get<Dictionary<string, dynamic>>();
                    String str = data["img"];
                    Byte[] imgBytes = Convert.FromBase64String(str.Split(',')[1]);
                    Size size = new Size(data["w"], data["h"]);
                    Point position = new Point(data["x"], data["y"]);

                    if (size.Width > 0 && size.Height > 0)
                    {
                        canvasInteraction.DrawImage(position, size, imgBytes);
                    }

                });

            });

            lio.on("line", async (o) =>
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    Event e = (Event)o;
                    Dictionary<string, dynamic> data = e.get<Dictionary<string, dynamic>>();

                    double fromX, fromY, toX, toY;
                    try { fromX = data["fromX"]; } catch { fromX = 0; }
                    try { fromY = data["fromY"]; } catch { fromY = 0; }
                    try { toX = data["toX"]; } catch { toX = 0; }
                    try { toY = data["toY"]; } catch { toY = 0; }

                    Point fromPoint = new Point(fromX * Canvas.Width, fromY * Canvas.Height);
                    Point toPoint = new Point(toX * Canvas.Width, toY * Canvas.Height);
                    int thickness = data.ContainsKey("thinckness") ? data["thickness"] : 5;
                    canvasInteraction.DrawLine(fromPoint, toPoint, data["color"], thickness);
                });

            });

            lio.on("message", async (o) =>
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    Event e = (Event)o;
                    Dictionary<string, dynamic> data = e.get<Dictionary<string, dynamic>>();
                    User user = new User()
                    {
                        Name = data["author"]["Name"],
                        FirstName = data["author"]["FirstName"],
                        Role = data["author"]["Role"],
                        Mail = data["author"]["Mail"],
                        ID = data["author"]["ID"]
                    };
                    WriteMessage(user, data["text"], false);
                });

            });

            lio.onUserJoinRoom(async (o) =>
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    User user = (User)o;
                    WriteMessage(null, user.FirstName + " " + user.Name + " is now connected", false);
                });
            });

            lio.onUserLeftRoom(async (o) =>
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    User user = (User)o;
                    WriteMessage(null, user.FirstName + " " + user.Name + " is now disconnected", false);
                });
            });
        }
        private void WriteMessage(User author, string text, bool me)
        {
            if (author == null)
            {
                Grid grid = new Grid()
                {
                    HorizontalAlignment = HorizontalAlignment.Left,
                    Margin = new Thickness(3)
                };

                TextBlock block = new TextBlock()
                {
                    FontSize = 15,
                    Foreground = new SolidColorBrush(Colors.Black),
                    Text = text
                };

                grid.Children.Add(block);
                Tchat.Children.Add(grid);

                previousAuthor = null;
            }
            else
            {
                if (me)
                {
                    Grid grid = new Grid()
                    {
                        HorizontalAlignment = HorizontalAlignment.Right,
                        Margin = new Thickness(3)
                    };

                    TextBlock textblock = new TextBlock()
                    {
                        FontSize = 15,
                        Foreground = new SolidColorBrush(Colors.Black),
                        TextWrapping = TextWrapping.Wrap,
                        Text = text
                    };

                    Border rectangle = new Border()
                    {
                        BorderBrush = new SolidColorBrush(Color.FromArgb(255, 188, 199, 214)),
                        Background = new SolidColorBrush(Color.FromArgb(255, 224, 237, 255)),
                        BorderThickness = new Thickness(1),
                        Padding = new Thickness(7),
                        Child = textblock
                    };

                    grid.Children.Add(rectangle);
                    Tchat.Children.Add(grid);
                }
                else
                {
                    if (previousAuthor == null || !previousAuthor.Mail.Equals(author.Mail))
                    {
                        Grid grid2 = new Grid()
                        {
                            HorizontalAlignment = HorizontalAlignment.Left
                        };

                        TextBlock authorblock = new TextBlock()
                        {
                            FontSize = 15,
                            Foreground = new SolidColorBrush(Colors.Black),
                            Text = author.FirstName + " " + author.Name
                        };

                        grid2.Children.Add(authorblock);
                        Tchat.Children.Add(grid2);
                    }


                    Grid grid = new Grid()
                    {
                        HorizontalAlignment = HorizontalAlignment.Left,
                        Margin = new Thickness(3, 0, 3, 3)
                    };

                    TextBlock textblock = new TextBlock()
                    {
                        FontSize = 15,
                        Foreground = new SolidColorBrush(Colors.Black),
                        TextWrapping = TextWrapping.Wrap,
                        Text = text
                    };

                    Border rectangle = new Border()
                    {
                        BorderBrush = new SolidColorBrush(Color.FromArgb(255, 188, 199, 214)),
                        Background = new SolidColorBrush(Color.FromArgb(255, 254, 254, 254)),
                        BorderThickness = new Thickness(1),
                        Padding = new Thickness(7),
                        Child = textblock
                    };

                    grid.Children.Add(rectangle);
                    Tchat.Children.Add(grid);
                }

                previousAuthor = author;
            }

            // To go down the scrollbar
            ScrollViewer.UpdateLayout();
            ScrollViewer.ChangeView(0, double.MaxValue, 1);
        }