Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // получили сообщение
- - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
- NSError *jsonError;
- NSData *objectData = [message dataUsingEncoding:NSUTF8StringEncoding];
- id messageData = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError];
- if (jsonError) {
- // ошибка сериализации json
- return;
- }
- if ([messageData isKindOfClass:[NSMutableDictionary class]]) {
- // эвент
- [self eventHandler:messageData];
- }
- if ([messageData isKindOfClass:[NSMutableArray class]]) {
- // данные
- [self dataHandler:messageData];
- }
- }
- //-----------------------
- //эвенты
- // обработчик событий
- - (void)eventHandler:(NSDictionary *)message {
- // info
- if ([message[@"event"] isEqualToString:@"info"]) {
- [self infoEvent:message];
- return;
- }
- // subscribed
- if ([message[@"event"] isEqualToString:@"subscribed"]) {
- [self subscribedEvent:message];
- return;
- }
- // error
- if ([message[@"event"] isEqualToString:@"error"]) {
- [self closeSocket];
- [self openSocket];
- return;
- }
- }
- - (void)infoEvent:(NSDictionary *)message {
- if (![message[@"code"] isKindOfClass:[NSNull class]]) {
- // достаем код ошибки
- NSInteger code = [message[@"code"] integerValue];
- if (code == 20051) {
- // restart socket notify
- NSLog(@"restart socket notify");
- [self closeSocket];
- [self openSocket];
- }
- if (code == 20060) {
- // pause activity for 20061 code
- NSLog(@"pause activity for 20061 code");
- }
- if (code == 20061) {
- // resume activity
- NSLog(@"resume activity");
- }
- }
- }
- - (void)subscribedEvent:(NSDictionary *)message {
- // ордера
- if ([message[@"channel"] isEqualToString:@"book"]) {
- self.channel = [message[@"chanId"] integerValue];
- }
- }
- // ---------------------
- // данные
- // обработчик данных
- - (void)dataHandler:(NSArray *)message {
- // достаем канал
- NSInteger chanelID = [message[0] integerValue];
- // стаканы
- if (chanelID == self.channel) {
- [self bookData:message];
- }
- }
- - (void)bookData:(NSArray *)message {
- // Algorithm to create and keep a book instance updated
- //
- // 1. subscribe to channel
- // 2. receive the book snapshot and create your in-memory book structure
- //
- // смотрим не Heartbeating ли это
- if ([message[1] isKindOfClass:[NSString class]]) {
- NSString *hb = message[1];
- if ([hb isEqualToString:@"hb"]) {
- return;
- }
- }
- // снапшот приходит тока 1 раз по этому я его тока первый раз проверяю
- // и при первом запуске или рестарте сокета self.snapshot = YES
- // тип данных PBooksItem - собственно сам итем стакана массив
- // @implementation PBooksItem
- // - (instancetype)initWithArray:(NSArray *)item {
- // self = [super init];
- // if (self) {
- // self.price = [self floatItem:item[0]]; цена
- // self.count = [self integerItem:item[1]]; количество ордеров - не юзаем
- // self.amount = [self floatItem:item[2]]; обьем в битках
- // }
- // return self;
- //}
- // определяем снапшот это или апдейт
- if (self.snapshot) {
- // snapshot
- self.snapshot = NO;
- for (NSArray *item in message[1]) {
- PBooksItem *order = [[PBooksItem alloc] initWithArray:item];
- if (order.amount > 0) {
- [self.bidTrades addObject:order];
- }
- if (order.amount < 0) {
- [self.askTrades addObject:order];
- }
- }
- } else {
- // update
- // 3. when count > 0 then you have to add or update the price level
- // 3.1 if amount > 0 then add/update bidTrades
- // 3.2 if amount < 0 then add/update askTrades
- //
- // 4. when count = 0 then you have to delete the price level.
- // 4.1 if amount = 1 then remove from bidTrades
- // 4.2 if amount = -1 then remove from askTrades
- // NSLog(@"update");
- NSArray *item = message[1];
- PBooksItem *order = [[PBooksItem alloc] initWithArray:item];
- // 3. when count > 0 then you have to add or update the price level
- if (order.count > 0) {
- if (order.amount > 0) {
- // 3.1 if amount > 0 then add/update bidTrades
- [self addUpdateBids:order];
- }
- if (order.amount < 0) {
- // 3.2 if amount < 0 then add/update askTrades
- [self addUpdateAsks:order];
- }
- }
- // 4. when count = 0 then you have to delete the price level.
- if (order.count == 0) {
- if (order.amount == 1) {
- // 4.1 if amount = 1 then remove from bidTrades
- [self removeFromBids:order];
- }
- if (order.amount == -1) {
- // 4.2 if amount = -1 then remove from askTrades
- [self removeFromAsks:order];
- }
- }
- }
- }
- - (void)addUpdateBids:(PBooksItem *)order {
- if (self.bidTrades.count == 0) {
- // добавляем если массив стакана bidTrades пуст
- [self.bidTrades addObject:order];
- return;
- }
- // проверяем первый
- PBooksItem *bidFirst = self.bidTrades[0];
- if (order.price == bidFirst.price) {
- // replace
- self.bidTrades[0] = order;
- return;
- }
- if (order.price > bidFirst.price) {
- // add order first
- [self.bidTrades insertObject:order atIndex:0];
- return;
- }
- // проверяем последний
- PBooksItem *bidLast = self.bidTrades[self.bidTrades.count - 1];
- if (order.price == bidLast.price) {
- // replace
- self.bidTrades[self.bidTrades.count - 1] = order;
- return;
- }
- if (order.price < bidLast.price) {
- // add order last
- [self.bidTrades addObject:order];
- return;
- }
- // находим куда вставить или заменить
- for (NSUInteger index = 0; index <= self.bidTrades.count - 2; index++) {
- PBooksItem *bid = self.bidTrades[index];
- if (order.price == bid.price) {
- // replace
- self.bidTrades[index] = order;
- return;
- }
- PBooksItem *bidNext = self.bidTrades[index + 1];
- if (order.price < bid.price && order.price > bidNext.price) {
- // insert
- [self.bidTrades insertObject:order atIndex:index + 1];
- return;
- }
- }
- }
- - (void)addUpdateAsks:(PBooksItem *)order {
- if (self.askTrades.count == 0) {
- [self.askTrades addObject:order];
- return;
- }
- PBooksItem *askFirst = self.askTrades[0];
- if (order.price == askFirst.price) {
- // replace
- self.askTrades[0] = order;
- return;
- }
- if (order.price < askFirst.price) {
- // add order first
- [self.askTrades insertObject:order atIndex:0];
- return;
- }
- PBooksItem *askLast = self.askTrades[self.askTrades.count - 1];
- if (order.price == askLast.price) {
- // replace
- self.askTrades[self.askTrades.count - 1] = order;
- return;
- }
- if (order.price > askLast.price) {
- // add order last
- [self.askTrades addObject:order];
- return;
- }
- // находим куда вставить или заменить
- for (NSUInteger index = 0; index <= self.askTrades.count - 2; index++) {
- PBooksItem *ask = self.askTrades[index];
- if (order.price == ask.price) {
- // replace
- self.askTrades[index] = order;
- return;
- }
- PBooksItem *askNext = self.askTrades[index + 1];
- if (order.price > ask.price && order.price < askNext.price) {
- // insert
- [self.askTrades insertObject:order atIndex:index + 1];
- return;
- }
- }
- }
- - (void)removeFromBids:(PBooksItem *)order {
- for (NSUInteger index = 0; index <= self.bidTrades.count - 1; index++) {
- PBooksItem *bid = self.bidTrades[index];
- if (bid.price == order.price) {
- [self.bidTrades removeObjectAtIndex:index];
- return;
- }
- }
- }
- - (void)removeFromAsks:(PBooksItem *)order {
- for (NSUInteger index = 0; index <= self.askTrades.count - 1; index++) {
- PBooksItem *ask = self.askTrades[index];
- if (ask.price == order.price) {
- [self.askTrades removeObjectAtIndex:index];
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement