Java: WebSocket server won't send data to client properly - websocket

Handshakes are done correctly and the server can decode the data coming from the client, but the client closes the connection when I try to send data to it.
I've been using http://websocket.org/echo.html as the client w. latest versions of Firefox & Chrome.
Here's the data frame I'm trying to send:
129 10000001
4 100
116 1110100
101 1100101
115 1110011
116 1110100
-------
fin:true
opcode:1
len:4
masked:false
masks:[0, 0, 0, 0]
payload:test
?♦test
http://tools.ietf.org/html/rfc6455#section-5
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
*/
And the server side method responsible for sending data to the client:
public void sendData(Socket socket, byte[] dataBytes){
System.out.println(java.util.Arrays.toString(dataBytes));
//[-127, 4, 116, 101, 115, 116]
for(byte b:dataBytes) System.out.println(Integer.toString((int)0xff&b,2));
/*
10000001
100
1110100
1100101
1110011
1110100
*/
try{
InputStream data = new ByteArrayInputStream(dataBytes);
OutputStream out = socket.getOutputStream();
//tested with ByteArrayOutputStream and written data == dataBytes
//out.write((byte)0x00); //tried with and without this
if ( data != null )
{
// tried also out.write(dataBytes) intstead of this
byte[] buff = new byte[2048];
while (true)
{
int read = data.read( buff, 0, 2048 );
if (read <= 0)
break;
out.write( buff, 0, read );
}
}
//out.write(-1);
//out.write((byte)0xFF);
out.flush();
//out.close();
if ( data != null )
data.close();
}catch(Exception e){
e.printStackTrace();
sockets.remove(socket);
}
}

Some questions:
Do you wait for the connection to open fully before sending from the server?
Can you capture the stream using wireshark and see what's actually on the wire?
In Chrome's Javascript console do you see any WebSocket related errors?
In your onclose handler for the Javascript websocket object, can you console.log the values of code and reason from the event?
Like this:
ws.onclose = function (e) {
console.log("closed - code " + e.code + ", reason " + reason);
}

Your issue was (probably) using the old protocol. Use the newer one...
#see
http://web-sockets.org
Has working source for a client and server (in Java).

Related

Laravel: relationship between siblings

I have 4 tables flats, residents, floors & resident_floors. The relations between them is as follows:
flats:
id | name
-----------------------------
1 | Flat -1
2 | Flat -2
...
flat_residents:
id | flats_id | name
-----------------------------
1 | 1 | Resident - 1
2 | 2 | Resident - 2
3 | 3 | Resident - 3
...
flat_floors:
id | flats_id
-----------------------------
1 | 101
2 | 102
3 | 201
4 | 202
...
flat_resident_floors:
id | residents_id | floors_id
-----------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
4 | 3 | 4
I am trying to create the relationship between them to display the data as follows:
Flat / Floor(s) | Resident
1 / 101, 102 | Resident - 1
2 / 201 | Resident - 2
3 / 202 | Resident - 3
Resident.php
public function floors()
{
return $this->hasManyThrough(Floor::class, ResidentFloor::class, 'floors_id', 'id');
}
Here is the query which is being generated:
SELECT * FROM floors
INNER JOIN flat_resident_floors ON flat_resident_floors.id = floors.id
WHERE flat_resident_floors.floors_id = ?
Where as it should be:
SELECT * FROM floors
INNER JOIN flat_resident_floors ON flat_resident_floors.floors_id = floors.id
WHERE flat_resident_floors.residents_id = ?
I don't understand what or where am I doing wrong..?
Don't think about the underlying SQL so much, instead use the Eloquent relationships to your advantage. In this case, it seems that Flat should hasMany(Floor::class) and Floor should hasMany(Resident::class). Your Flat then hasManyThrough relationship with Resident which writes itself (without needing a pivot table as you are trying to do).
class Floor
{
public function residents()
{
return $this->hasMany(Resident::class);
}
public function flat()
{
return $this->belongsTo(Flat::class);
}
}
class Resident
{
public function floor()
{
return $this->belongsTo(Floor::class);
}
}
class Flat
{
public function floors()
{
return $this->hasMany(Floor::class);
}
public function residents()
{
return $this->hasManyThrough(Resident::class, Floor::class);
}
}

Multiple Scheduled in the same method

I'm working with #Scheduled annotations. I need to run the method in different moments, exactly each 2 and 5 seconds.
How can I do it?
In this moment my code is the next:
#Scheduled(cron = "${cron.startdate}")
public void check() {
LOGGER.info("1 - Check DB");
}
and the application.yml:
cron:
startdate: 0/2 * * * * *
My configuration will be executed each 2 seconds, but I need it to be executed each 5 seconds too.
The output should be:
[11:20:00] | 1 - Check DB
[11:20:02] | 1 - Check DB
[11:20:04] | 1 - Check DB
[11:20:05] | 1 - Check DB
[11:20:06] | 1 - Check DB
[11:20:08] | 1 - Check DB
[11:20:10] | 1 - Check DB
[11:20:12] | 1 - Check DB
.
.
.
Thanks guys.
I think #Scheduled(cron = "0/2,0/5 * * * * *") should work.

Configuring Sass Lint for BEM

So i'm moving to writing my sass to the BEM convention. I've used the sass-lint configuration generator to create my config and only edited the class-name-format's - convention: to strictbem however I'm still having some issues with it.
Maybe I'm misinterpreting BEM?
Error:
[sass-lint] Class '.bus__tyre--front' should be written in BEM (Block
Element Modifier) format (class-name-format)
<element class="bus__tyre--front">
Sass:
.bus {
position: relative;
&__tyre {
position: absolute;
&--front {
bottom: -22px;
right: 3%;
width: 17%;
}
}
}
sass-lint.yml:
# sass-lint config generated by make-sass-lint-config v0.1.2
#
# The following scss-lint Linters are not yet supported by sass-lint:
# DisableLinterReason, ElsePlacement, PropertyCount, SelectorDepth
# SpaceAroundOperator, TrailingWhitespace, UnnecessaryParentReference, Compass::*
#
# The following settings/values are unsupported by sass-lint:
# Linter Indentation, option "allow_non_nested_indentation"
# Linter Indentation, option "character"
# Linter NestingDepth, option "ignore_parent_selectors"
# Linter PropertySortOrder, option "min_properties"
# Linter PropertySortOrder, option "separate_groups"
# Linter SpaceBeforeBrace, option "allow_single_line_padding"
# Linter VendorPrefix, option "identifier_list"
files:
include: '**/*.scss'
options:
formatter: stylish
merge-default-rules: false
rules:
bem-depth:
- 0
- max-depth: 1
border-zero:
- 1
- convention: zero
brace-style:
- 1
- allow-single-line: true
class-name-format:
- 1
- convention: strictbem
clean-import-paths:
- 1
- filename-extension: false
leading-underscore: false
empty-line-between-blocks:
- 1
- ignore-single-line-rulesets: true
extends-before-declarations: 1
extends-before-mixins: 1
final-newline:
- 1
- include: true
force-attribute-nesting: 1
force-element-nesting: 1
force-pseudo-nesting: 1
function-name-format:
- 1
- allow-leading-underscore: true
convention: hyphenatedlowercase
hex-length:
- 1
- style: short
hex-notation:
- 1
- style: lowercase
id-name-format:
- 1
- convention: hyphenatedlowercase
indentation:
- 1
- size: 2
leading-zero:
- 1
- include: false
mixin-name-format:
- 1
- allow-leading-underscore: true
convention: hyphenatedlowercase
mixins-before-declarations: 1
nesting-depth:
- 1
- max-depth: 3
no-color-keywords: 1
no-color-literals: 1
no-css-comments: 1
no-debug: 1
no-duplicate-properties: 1
no-empty-rulesets: 1
no-extends: 0
no-ids: 1
no-important: 1
no-invalid-hex: 1
no-mergeable-selectors: 1
no-misspelled-properties:
- 1
- extra-properties: []
no-qualifying-elements:
- 1
- allow-element-with-attribute: false
allow-element-with-class: false
allow-element-with-id: false
no-trailing-zero: 1
no-transition-all: 0
no-url-protocols: 1
no-vendor-prefixes:
- 1
- additional-identifiers: []
excluded-identifiers: []
placeholder-in-extend: 1
placeholder-name-format:
- 1
- convention: hyphenatedlowercase
property-sort-order:
- 1
- ignore-custom-properties: false
property-units:
- 1
- global:
- ch
- em
- ex
- rem
- cm
- in
- mm
- pc
- pt
- px
- q
- vh
- vw
- vmin
- vmax
- deg
- grad
- rad
- turn
- ms
- s
- Hz
- kHz
- dpi
- dpcm
- dppx
- '%'
per-property: {}
quotes:
- 1
- style: single
shorthand-values:
- 1
- allowed-shorthands:
- 1
- 2
- 3
single-line-per-selector: 1
space-after-bang:
- 1
- include: false
space-after-colon:
- 1
- include: true
space-after-comma:
- 1
- include: true
space-before-bang:
- 1
- include: true
space-before-brace:
- 1
- include: true
space-before-colon: 1
space-between-parens:
- 1
- include: false
trailing-semicolon: 1
url-quotes: 1
variable-for-property:
- 0
- properties: []
variable-name-format:
- 1
- allow-leading-underscore: true
convention: hyphenatedlowercase
zero-unit: 1
Judging by #335 and #319 in the scss-lint repo, it looks like you need to change:
class-name-format:
- 1
- convention: strictbem
to:
class-name-format:
- 1
- convention: hyphenatedbem
I hope that helps!
Instead of hyphenated_BEM it should be hyphenatedbem.
Example from the sass-lint docs

YAML parsing - c++

I'm very new to YAML and what to parse a file I have made and verified is of correct structure. Here is the YAML:
---
DocType: DAQ Configuration File
# Creation Date: Wed Sep 21 10:34:06 2016
File Settings:
- Configuration Path: /mnt/sigma/daqengine/config/
- Configuration Name: daqengine.yaml
- Build File Path: ./bld
- Log File Path: ./log
- Record H5 Files: true
Engine Data:
- Make: EOS
- Model: M280
- Number of DAQs: 1
DAQ01 Settings:
- IP Address: 192.168.116.223
- Log DAQ Internal Temps: true
- T0 Temp. Limit: 55
- T0 Clear OTC Level Temp.: 50
- T0 Sample Averaging: 5
- T1 Temp. Limit: 60
- T1 Clear OTC Level Temp.: 55
- T1 Sample Averaging: 2
- No. Layers Used: 1
DAQ01 Layer 0 Settings:
- Model: AI-218
- Sample Rate: 50000
- Channels Used: 8
- Channel01: HF Temp
- Channel02: On-Axis PD
- Channel03: Off-Axis PD
- Channel04: X-Position
- Channel05: Y-Position
- Channel06: LDS
- Channel07: Laser Power
- Channel08: Unused
...
I need to extract the sequenced values to each key it is tagged to in C++.

Ruby's YAML only loads first Records from a large File, why?

Okay, I have the following YAML file that was generated by using yaml_db for Rails. So this is basically an autogenerated export of my Rails database:
---
admins:
columns:
- id
- username
- email
- encrypted_password
- password_salt
- sign_in_count
- current_sign_in_at
- last_sign_in_at
- current_sign_in_ip
- last_sign_in_ip
- failed_attempts
- unlock_token
- locked_at
- created_at
- updated_at
records:
- - 1
-
- lalala#lalala.at
- $2a$10$dZU50HD6paWS7EjKuWAruOFdwt9eqxiNTRh/D4sj8cqSzy5gjYd2i
- $2a$10$dZU50HD6paWS7EjKuWAruO
- 86
- 2011-01-27 07:37:45 Z
- 2011-01-26 13:27:13 Z
- 12.34.56.78
- 12.34.56.78
- 0
-
-
- 2010-12-23 09:20:46 Z
- 2011-01-27 07:37:45 Z
- - 2
- admin
- lalalalala#lalalala.com
- $2a$10$3DML64hdCCvG90bnhIpN/unEEm6C.a9FqGrAFlFHU0.2D54DSQ1Ni
- $2a$10$3DML64hdCCvG90bnhIpN/u
- 1
- 2011-01-21 09:52:14 Z
- 2011-01-21 09:52:14 Z
- 12.34.56.78
- 12.34.56.78
- 0
-
-
- 2011-01-05 14:29:49 Z
- 2011-01-21 09:52:14 Z
---
experiments:
columns:
- id
- description
- startdate
- enddate
- maps_base_URI
- maps_count
- queries_count
- proposals_count
- created_at
- updated_at
.......
Now when I try to load this YAML file in Ruby with:
file = YAML.load(File.open("data-2011-01-27.yml"))
It doesn't load more than the first admin, not even the experiments:
ruby-1.9.2-p0 > file.keys
=> ["admins"]
ruby-1.9.2-p0 > file["admins"]["records"].count
=> 1
Why is that? I would assume that the autogenerated .yml file is syntactically correct? When I run rake:db:dump and rake:db:load it works just fine.
Three hyphens (---) separate multiple documents. See YAML.load_stream to load them all:
documents = YAML.load_stream(open("data-2011-01-27.yml")).documents
documents.map(&:keys)
#=> [["admins"], ["experiments"]]

Resources