Skip to content

Instantly share code, notes, and snippets.

View jbarros35's full-sized avatar

10x developer jbarros35

  • Lisbon
View GitHub Profile
@jbarros35
jbarros35 / gist:826ff8be8a4c0377b4a3c82371d6a80d
Created April 11, 2018 13:01
Open websocket on browser for a server running ws Node
// if user is running mozilla then use it's built-in WebSocket
window.WebSocket = window.WebSocket || window.MozWebSocket;
var connection = new WebSocket('wss://localhost:443', 'echo-protocol');
connection.onopen = function () {
// connection is opened and ready to use
console.log('client opened socket');
$scope.send();
};
@jbarros35
jbarros35 / gist:96a82451a46258b30a3135dbeeddbeef
Created April 11, 2018 13:04
Open Websocket into Node JS server
require('dotenv').config();
var http = require('http');
var https = require('https');
var fs = require('fs');
const WebSocket = require('ws');
Tail = require('tail').Tail;
var privateKey = fs.readFileSync('./key.pem', 'utf8');
var certificate = fs.readFileSync('./cert.pem', 'utf8');
var data = $.param({
attr1: $scope.attr1,
attr2: $scope.attr2
});
var config = {
headers : {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
#create new node with json genesis
geth --datadir ./myDataDir --networkid 1114 init ./myGenesis.json console 2>> myEth.log
#start geth for ws without ipc support
nohup geth --datadir ./myDataDir --networkid 1114 --ws --wsorigins="*" --wsapi "db,eth,net,ssh,miner,web3,personal,admin" --rpc --rpcapi "web3,eth,personal,miner,net,txpool" --rpccorsdomain "*" --ipcdisable &
#geth attach without ipc
geth attach http://localhost:85451
#geth transfer
@jbarros35
jbarros35 / gist:2986411b5aca7b0b60949f7762776b17
Created April 12, 2018 22:23
Angular execute a function every 60 seconds
$interval(function() {
var d = Date();
console.log('----- sending transaction 1 wei '+d+'------');
}, 60000);
$http.post('url', data, config)
.success(function (data, status, headers, config) {
if (data) {
console.log(data);
}
})
.error(function (data, status, header, config) {
console.log(data.errorMsg);
});
@jbarros35
jbarros35 / gist:65bfa62c302b11992a64c5496e358813
Created April 13, 2018 09:35
Swift flatmap and regex matches
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let results = regex.matches(in: text,
range: NSRange(text.startIndex..., in: text))
return results.map {
String(text[Range($0.range, in: text)!])
}
} catch let error {
@jbarros35
jbarros35 / gist:f0dd9bb38cd4d1cc7ccc1ffdbcbefe6c
Created April 13, 2018 09:37
Swift a failable initializer, will fails if any of those params would nil
struct User {
let id: Int
let name: String
let username: String
init?(dictionary: Dictionary<String: Any>) {
guard
let id = dictionary[“id”] as? Int,
let name = dictionary[“name”] as? String,
let username = dictionary[“username”] as? String
else {
@jbarros35
jbarros35 / gist:e4d4d831305e2107997c25e6c4182dd1
Last active April 16, 2018 11:03
Simple count words with dictionary in Swift
var dictionary = [String: Int]()
var counts = "all for all fun fun funny".components(separatedBy:" ").map({
var counter = dictionary[$0] ?? 0
dictionary[$0] = counter + 1
})
print(dictionary)
@jbarros35
jbarros35 / gist:8c41f1a147dcc715e9c0b6926be4f60b
Last active April 16, 2018 12:52
Swift code printing X in the screen.
let matrixSize = 10
for y in 0..<matrixSize {
var line = ""
for x in 0..<matrixSize {
let point = (x, y)
switch point {
case let (x, y) where x == y:
line.append("#")
case let (x, y) where y == matrixSize-1 || y == 0 || x == 0 || x == matrixSize-1:
line.append("#")