博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
amazon 设计 9 chat server
阅读量:6125 次
发布时间:2019-06-21

本文共 3255 字,大约阅读时间需要 10 分钟。

Solution:

Apparently, I am too naive and I think too few. Here's the detailed solution.

What is our chat server?

This is something you should discuss with your interviewer, but let’s make a couple of as- sumptions: imagine we’re designing a basic chat server that needs to support a small number of users. People have a contact list, they see who is online vs offline, and they can send text-based messages to them. We will not worry about supporting group chat, voice chat, etc. We will also assume that contact lists are mutual: I can only talk to you if you can talk to me. Let’s keep it simple.

What specific actions does it need to support?

  • User A signs online
  • User A asks for their contact list, with each person’s current status
  • Friends of User A now see User A as online
  • User A adds User B to contact list
  • User A sends text-based message to User B
  • User A changes status message and/or status type
  • User A removes User B
  • User A signs offline

What can we learn about these requirements?

We must have a concept of users, add request status, online status, and messages.

What are the core components?

We’ll need a database to store items and an "always online" application as the server. We might recommend using XML for the communication between the chat server and the clients, as it’s easy for a person and a machine to read.

What are the key objects and methods?

We have listed the key objects and methods below. Note that we have hidden many of the details, such as how to actually push the data out to a client.

 

enum StatusType {    online, offline, away;} class Status {    StatusType status_type;    String status_message;} class User {    String username;    String display_name;    User[] contact_list;    AddRequest[] requests;     boolean updateStatus(StatusType stype,             String message) {    };     boolean addUserWithUsername(String name);     boolean approveRequest(String username);     boolean denyRequest(String username);     boolean removeContact(String username);     boolean sendMessage(String username,             String message);} // Holds data that from_user would like to add to_userclass AddRequest {    User from_user;    User to_user;} class Server {    User getUserByUsername(String username);}

What problems would be the hardest to solve (or the most interesting)?

    1. How do we know if someone is online—I mean, really, really know?
      While we would like users to tell us when they sign off, we can’t know for sure. A user's connection might have died, for example. To make sure that we know when a user has signed off, we might try regularly pinging the client to make sure it’s still there.
    2. How do we deal with conflicting information?
      We have some information stored in the computer’s memory and some in the database. What happens if they get out of sync? Which one is "right"?
    3. How do we make our server scale?
      While we designed out chat server without worrying -too much- about scalability, in real life this would be a concern. We’d need to split our data across many servers, which would increase our concern about out of sync data.
    4. How we do prevent denial of service attacks?
      Clients can push data to us—what if they try to DOS us? How do we prevent that?

转载于:https://www.cnblogs.com/leetcode/p/3906253.html

你可能感兴趣的文章
爬虫豆瓣top250项目-开发文档
查看>>
Elasticsearch增删改查
查看>>
oracle归档日志增长过快处理方法
查看>>
有趣的数学书籍
查看>>
teamviewer 卸载干净
查看>>
多线程设计模式
查看>>
解读自定义UICollectionViewLayout--感动了我自己
查看>>
SqlServer作业指定目标服务器
查看>>
UnrealEngine4.5 BluePrint初始化中遇到编译警告的解决办法
查看>>
User implements HttpSessionBindingListener
查看>>
抽象工厂方法
查看>>
ubuntu apt-get 安装 lnmp
查看>>
焊盘 往同一个方向增加 固定的长度方法 总结
查看>>
eclipse的maven、Scala环境搭建
查看>>
架构师之路(一)- 什么是软件架构
查看>>
jquery的冒泡和默认行为
查看>>
USACO 土地购买
查看>>
【原创】远景能源面试--一面
查看>>
B1010.一元多项式求导(25)
查看>>
10、程序员和编译器之间的关系
查看>>